Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python MySQLdb not inserting data

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 
like image 620
BubbleGuppies Avatar asked Feb 18 '13 04:02

BubbleGuppies


People also ask

Does MySQLdb work with python3?

MySQLdb module, a popular interface with MySQL is not compatible with Python 3.

What is Python MySQLdb package?

What is MYSQLdb? MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2. 0 and is built on top of the MySQL C API. Packages to Install mysql-connector-python mysql-python.

How do you call SP in Python?

To call a stored procedure from a Python application, use ibm_db. callproc function. The procedure that you call can include input parameters (IN), output parameters (OUT), and input and output parameters (INOUT).


1 Answers

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

like image 164
Ketouem Avatar answered Oct 24 '22 11:10

Ketouem