我在使用MySQL或Python时遇到了问题,似乎无法隔离问题。INSERT似乎只在脚本运行的最后一次,不存储在数据库中。在
我有这个剧本:import MySQLdb
db = MySQLdb.connect(host="localhost", user="user", passwd="password", db="example")
dbcursor = db.cursor()
dbcursor.execute("select * from tablename")
temp = dbcursor.fetchall()
print 'before: '+str(temp)
dbcursor.execute('INSERT INTO tablename (data1, data2, data3) VALUES ("1", "a", "b")')
dbcursor.execute("select * from tablename")
temp = dbcursor.fetchall()
print 'after: '+str(temp)
我第一次运行时得到的输出:
^{pr2}$
问题是,如果我再次运行它,before应该已经有了条目,而after却没有中断(数据1是主键)。在>>>
before: ()
after: ((1L, 'a', 'b'),)
>>>
before: ()
after: ((1L, 'a', 'b'),)
>>>
before: ()
after: ((1L, 'a', 'b'),)
如果我尝试在同一个脚本中运行insert命令两次,它将中断(“主键的重复条目”)
你知道这里会发生什么吗?在