Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python MySQL: Not showing inserted records [duplicate]

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.

like image 478
MrZander Avatar asked Apr 04 '12 07:04

MrZander


1 Answers

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.

like image 92
Nolen Royalty Avatar answered Oct 10 '22 21:10

Nolen Royalty