[DB-SIG] Question about MySQL insert function

Kiwi Golf ioboe at hotmail.com
Sun Jul 30 07:49:16 CEST 2006

Dear all,

Following is the example code I tried. I got a problem that the table is 
created however the data can not insert into MySQL server. Any idea how to 
fix this?

Thank you very much!
import sys
import MySQLdb

# connect to the MySQL server
    conn = MySQLdb.connect (host = "localhost",
                            user = "root",
                            passwd = "1234",
                            db = "animal")
except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit (1)

# create the animal table and populate it
    cursor = conn.cursor ()
    cursor.execute ("DROP TABLE IF EXISTS animal1")
    cursor.execute ("""
        CREATE TABLE animal1
                   name CHAR(40),
                   category CHAR(40)

    cursor.execute ("""
        INSERT INTO animal1 (name, category)
                   ('snake', 'reptile'),
                   ('frog', 'amphibian'),
                   ('tuna', 'fish'),
                   ('racoon', 'mammal')
    print "%d rows were inserted" % cursor.rowcount

    cursor.close ()

except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit (1)

    conn.close ()
    sys.exit (0)

Express yourself instantly with MSN Messenger! Download today - it's FREE! 

More information about the DB-SIG mailing list