Problem With Insert with MySQLdb

David Mitchell dave at dbmdata.com
Sun Oct 30 18:44:32 EST 2005


Hello,

I am a complete beginner with Python. I've managed to get mod_python up and
running with Apache2 and I'm trying to a simple insert into a table in a
MySQL database. 

I'm using the MySQLdb library for connectivity. I can read from the database
no problem, but when I do an insert, the value never gets added to the
database, even though there is no error, and the SQL is fine (I print out
the SQL statement in the function). When I copy and paste the sql from my
browser and insert directly into MySQL, it works fine.

Here is the function in question:

def add(req):
	db = MySQLdb.connect(host="intranet", user="root", passwd="",
db="intranet")
	# create a cursor
	cursor = db.cursor()
	# execute SQL statement
	
	sql = "INSERT INTO category (category_name) VALUES ('" +
req.form['category'] + "')"
	cursor.execute(sql)
	return sql


The SQL it is outputting is:

INSERT INTO category (category_name) VALUES ('Test')

Am I doing something obviously incorrect here?

Thanks,

Dave




More information about the Python-list mailing list