Python + MySQLdb怪异问题 [英] Python+MySQLdb weird problem

查看:46
本文介绍了Python + MySQLdb怪异问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用MySQLdb库时遇到问题.我正在尝试将值插入表中,但MySQL没有显示它们.更奇怪的是,当我使用命令提示符手动插入值时,主键正在更改.

I have a problem using the MySQLdb library. I'm trying to insert values into my table but MySQL isn't showing them. What's more strange is the primary key is changing when I manually insert values using command prompt.

给你看一个例子:

'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脚本正在删除内容...

I manually insert some value at 786 and 787 (primary key), then I run my python script, stopping it after I got 4 values. I type in 'SELECT * from table' and I see no changes. Then I inset another value (manually) and it shows a new primary key '792'. It seems like the python script is deleting things...

这是我的python代码:

Here's my python code:

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()

推荐答案

启用自动提交或提交更改,请参见:我的数据不见了!

Turn on autocommit or commit your changes, see: My data disappeared!

这篇关于Python + MySQLdb怪异问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆