[DB-SIG] Question about MySQL insert function
Chris Curvey
ccurvey at gmail.com
Mon Jul 31 00:36:19 CEST 2006
Just wondering...do you need to commit() ?
On 7/30/06, Kiwi Golf <ioboe at hotmail.com> wrote:
>
> 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!
> Chloe
> --------------------------------------------
> import sys
> import MySQLdb
>
> # connect to the MySQL server
> try:
> 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
> try:
> 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)
> VALUES
> ('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!
> http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
>
> _______________________________________________
> DB-SIG maillist - DB-SIG at python.org
> http://mail.python.org/mailman/listinfo/db-sig
>
--
The short answer is "Yes." The long answer is "No."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/db-sig/attachments/20060730/f67ca37f/attachment.htm
More information about the DB-SIG
mailing list