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.

Here's an example:

'786', '2011-02-16 14:52:38', NULL
'787', '2011-02-16 14:52:52', NULL
'792', '2011-02-16 14:53:25', NULL

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...

Here's my python code:

            conn = MySQLdb.connect(host = "", 
                                   user = "root", 
                                   passwd = "eBao1234", 
                                   db = "test")
            cursor = conn.cursor()
            print 'data base connected'
                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])
            except KeyboardInterrupt:
                print "\nInterrupted."
                raw_input("Press enter to exit.")
        except Exception,e:
            print str(e)
            print 'error connecting to database'

Examine, or post here, the table definition of the table you are inserting.

If you have an auto increment field, then you cannot insert values in that.

If you are using transactions, you have to commit the transaction.

This article has been dead for over six months. Start a new discussion instead.