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')
推荐阅读
- 随笔7.21-涂改遗忘
- 307|307 和真实握手
- Python3|Python3 MySQL 数据库连接
- win10环境|win10环境 python3.6安装pycrypto-2.6.1的问题
- 分布式|《Python3网络爬虫开发实战(第二版)》内容介绍
- python3|python3 模块 包
- 《花古而多》(一)
- pymysql|pymysql 封装操作
- 改变的原动力只能是自己
- 我有一封《情书》予你