David Mitchell wrote:
> 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
>
Try either executing this first:
cursor.execute("set autocommit = 1")
or this afterwards:
cursor.execute("commit")
The idea is that transactions are not committed into the database
until you "commit" them - and if you hit problems halfway through
a sequence of updates that really shouldn't be left half
finished, you can execute "abort" instead (or just close the
connection), and none of them will be done.
Steve
--
http://mail.python.org/mailman/listinfo/python-list