我在使用MySQLdb库时遇到了问题。我试图插入值到我的表,但MySQL不显示它们。更奇怪的是,当我使用命令提示符手动插入值时,主键正在改变。Python + MySQLdb奇怪的问题
要告诉你一个例子:
'786', '2011-02-16 14:52:38', NULL
'787', '2011-02-16 14:52:52', NULL
'792', '2011-02-16 14:53:25', NULL
我手动插入在786和787(主键)一些值,然后我跑我的Python脚本,停药之后我得到了4个值。我输入'SELECT * from table',我看不到有任何变化。然后我插入另一个值(手动)并显示一个新的主键'792'。这似乎是python脚本被删除的东西...
这里是我的Python代码:
try:
conn = MySQLdb.connect(host = "127.0.0.1",
user = "root",
passwd = "eBao1234",
db = "test")
cursor = conn.cursor()
print 'data base connected'
try:
while 1:
localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
i = i + 1
aRead = ser.readline()
aSplit = aRead.split(",")
if aSplit[0] == "t":
print str(i) + ", " + localtime + ", " + aSplit[1]
tableName = "sampdb"
dbquery = "INSERT INTO %s (timestamp, tempReading) VALUES (NOW(), %s);" % (tableName, aSplit[1])
cursor.execute(dbquery)
except KeyboardInterrupt:
ser.close()
#csvResults.close()
#cursor.close()
#conn.close()
print "\nInterrupted."
raw_input("Press enter to exit.")
except Exception,e:
print str(e)
print 'error connecting to database'
cursor.close()
conn.close()
谢谢,conn.commit()工作! conn.autocommit()在哪里? – lamba 2011-02-16 15:57:29