Can't seem to insert rows into a MySQL table

Anthra Norell anthra.norell at tiscalinet.ch
Mon Mar 14 22:42:25 CET 2005


Try to use % instead of a comma (a Python quirk) and quotes around your
strings (a MySQL quirk):

   cursor.execute("INSERT INTO edict (kanji, kana, meaning) VALUES ('%s',
'%s', '%s')" % ("a", "b", "c") )

Frederic


----- Original Message -----
From: "grumfish" <nobody at nowhere.com>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Saturday, March 12, 2005 7:24 PM
Subject: Can't seem to insert rows into a MySQL table


> I'm trying to add a row to a MySQL table using insert. Here is the code:
>
> connection = MySQLdb.connect(host="localhost", user="root", passwd="pw",
> db="japanese")
> cursor = connection.cursor()
> cursor.execute("INSERT INTO edict (kanji, kana, meaning) VALUES (%s, %s,
> %s)", ("a", "b", "c") )
> connection.close()
>
> After running, a SELECT * on the table shows no new rows added. Adding
> rows using the MySQL client works fine. With the Python script, nothing.
> There are no exceptions raised or any output at all. The rowcount of the
> cursor is 1 after the execute is 1 and the table's auto_increment value
> is increased for each insert done. Can anybody help? I'm fairly new to
> MySQL so I'm afraid its going to be a stupid oversight on my part. Thank
> you.
> --
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list