MySQL Forums
Forum List  »  Connector/Python

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.

Options: ReplyQuote


Subject
Written By
Posted
June 05, 2006 10:42AM
July 28, 2006 03:41AM
Re: Code not working
July 29, 2006 09:48PM
July 30, 2006 02:25PM
August 29, 2006 02:09PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.