我的python脚本中有两个mysql连接。例如:
conn1 = mdb.connect(server, user, pw, db)
conn2 = mdb.connect(server, user, pw, db)
#1. then, I execute "select" sql command to select table A by conn1,
#2. after that, I execute "update" sql command to update table A by conn2,
#3. finally, I execute "select" sql command again to select table A by conn1,
但最后,#3的结果与#1相同;然而,在#2之后,我看到数据在mysql workbench中更新。
有人知道为什么#3无法获取最新数据吗?
import MySQLdb as mdb
import time
conn1 = mdb.connect(SERVER, USER, PASSWORD, DB)
cur1 = conn.cursor()
count1 = cur.execute("SELECT trigger_time FROM trigger_set WHERE id=1")
data1 = cur.fetchall()
cur1.close()
print data1
conn2 = mdb.connect(SERVER, USER, PASSWORD, DB)
cur2 = conn2.cursor()
cur2.execute("update trigger_set set trigger_time = '2013/8/30 17:15' where id=1")
conn2.commit()
cur2.close()
cur1 = conn.cursor()
count1 = cur.execute("SELECT trigger_time FROM trigger_set WHERE id=1")
data1 = cur.fetchall()
print data1
答案 0 :(得分:0)
如果在再次选择之前提交conn1,或者只是将auto_commit设置为True,那么事情会有效。
conn1.commit()
这里已经讨论过:http://sourceforge.net/p/mysql-python/discussion/70461/thread/efea588e
BTW您的代码包含尚未定义的conn和cur变量..
答案 1 :(得分:0)
我认为问题在于为步骤1和3重复使用相同的光标`cur1'。尝试使用不同的光标或连接进行上一次选择。