python3|python3 pymysql只能读不能写数据库问题

在用pymysql执行数据库操作时发现select语句直接cur.execute(sql)时可以执行的,但是sql语句是update、insert、delete时,执行后数据库并没有进行更新,找了很久终于发现,在有数据库进行读写操作时需要在cur.execute(sql)执行后加上conn.commit()才可成功执行,对于pymysql来说,update、insert、delete语句都是属于一个事务,执行后必须commit才可以生效。
下面是例子:
try:
conn = pymysql.connect(host=['host'], user=['dbuser'],passwd=['dbpwd'],db=['name'], port=['port'],charset=['charset'])
cur = conn.cursor()
sql = "SELECT * FROM tableWHERE id = '%s'" % id
sql1="DELETE FROM order_status_transfer WHERE order_id ='%s' AND orig_state >0" % (id)
sql2 ="UPDATE orders SET ext_status1=10100000 WHERE id='%s'" % (id)
cur.execute(sql)
data = https://www.it610.com/article/cur.fetchall()#执行select语句可直接查询数据
cur.execute(sql1)
conn.commit()#执行DELETE语句必须commit才生效
cur.execute(sql2)
conn.commit() #执行UPDATE语句必须commit才生效
cur.close()
conn.close()
return data
except Exception:
【python3|python3 pymysql只能读不能写数据库问题】print('No requestsn')

    推荐阅读