Python MySQLdb not inserting data

BubbleGuppies picture BubbleGuppies · Feb 18, 2013 · Viewed 14k times · Source

ubuntu version: 12.10
mysql server version: 5.5.29-0
python version: 2.7

I am trying to use MySQLdb to insert data into my localhost mysql server. I don't get any errors when I run the script but the data isn't enter into my table. I view tables with phpmyadmin.

I tried going back to basics and following a tutorial but same result. The weird thing is that I can create and delete tables but not enter data.

The code is from the tutorial even reports that 4 rows were inserted. What is preventing data from being entered into the table when the script reports everything is fine??

cursor = conn.cursor () 
cursor.execute ("DROP TABLE IF EXISTS animal") 
cursor.execute (""" 
        CREATE TABLE animal 
        ( 
            name CHAR(40), 
            category CHAR(40) 
        ) 
    """) 
cursor.execute (""" 
        INSERT INTO animal (name, category) 
        VALUES 
            ('snake', 'reptile'), 
            ('frog', 'amphibian'), 
            ('tuna', 'fish'), 
            ('racoon', 'mammal') 
    """) 
print "%d rows were inserted" % cursor.rowcount 

Answer

Ketouem picture Ketouem · Feb 19, 2013

Add :

conn.commit()

at the bottom of your script.

On a side note, have a look at the following : http://mysql-python.sourceforge.net/MySQLdb.html