Python MySQL: Not showing inserted records

Go To StackoverFlow.com

9

I am using Python 2.7 with MySQLdb 32bit, alongside with MySQL 5.5.8 running local.

I am driving myself crazy over this, I have never seen anything like it.

Basically, I am inserting records into MySQL from Python via:

db=MySQLdb.connect(host="localhost",user="root", passwd="mypassword",db="python",port=3307)
cur=db.cursor()
cur.execute("INSERT INTO mytable(myfield) VALUES(%s);","somedata")

I have verified that it is connected properly and that it can successfully SELECT data from the database.

Here is the odd part: From MySQL Query Browser (GUI tool) AND from MySQL via CMD, I am unable to see the inserted records.

I can insert and select records from my Python Script, but they DO NOT show up in my database, it just returns Empty Set (0.00 sec)

And here is the really weird part: I can truncate and delete data from the GUI tool and from the Console.

To sum up: I can insert and select data from the Python Script. I can not see that data in MySQL. However, I can truncate and delete that data using MySQL.

I am completely lost at this point.

2012-04-04 07:07
by MrZander


19

If your code works in the console but not otherwise, I believe you need to add the line

db.autocommit(True)

After you connect or you need to do db.commit() after your inserts.

2012-04-04 07:11
by Nolen Royalty
Is the commit method not on db in this case - exhuma 2012-04-04 07:13
You are absolutely right @exhuma, my mistake - Nolen Royalty 2012-04-04 07:14
I wish I could have found that in the examples in the documentation... Thank you!!! I will accept your answer in approx. 4 minutes - MrZander 2012-04-04 07:17
Ads