发布网友 发布时间:2022-04-24 04:27
共2个回答
懂视网 时间:2022-04-30 14:16
#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语句
标签:
热心网友 时间:2022-04-30 11:24
第一种办法:
# 导入SQLite驱动:第二种办法:
使用 SQLalchemy 等ORM 的库。