python常用sql语句
时间:2021-07-01 10:21:17
帮助过:2人阅读
#coding=utf-8
2 import MySQLdb
3 conn = MySQLdb.Connect(host =
‘127.0.0.1‘,port=3306,user=
‘root‘,passwd=
‘‘,db=
‘test‘,charset=
‘utf8‘)
4 cursor =
conn.cursor()
5
6 try:
7 #插入数据
8 sql_insert =
"insert into orders(order_id,product_id) values (777,777)"
9 cursor.execute(sql_insert)
10 print cursor.rowcount
11
12 #修改数据
13 sql_update =
"update orders set order_id = 99999 where id=12"
14 cursor.execute(sql_update)
15 print cursor.rowcount
16
17 #删除数据
18 sql_delete =
"delete from orders where id = 3"
19 cursor.execute(sql_delete)
20 print cursor.rowcount
21
22 #提交
23 conn.commit()
24 except Exception as e:
25 print e
26 conn.rollback()
27
28 #关闭指针和连接
29 cursor.close()
30 conn.close()
python常用sql语句
标签: