Re: Code not working
Posted by:
Chloe C
Date: July 29, 2006 09:48PM
Hi, I also have a similar problem that the insert function does not work however the create table works fine. Following is the code example I try to run. Any ideas? Thanks ~
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)
Edited 1 time(s). Last edit at 07/29/2006 09:49PM by Chloe C.