Can't seem to insert rows into a MySQL table

deelan ggg at zzz.it
Sat Mar 12 14:28:59 EST 2005


grumfish wrote:
> 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()

which version of MySQLdb are you running? versions
1.1.6 and below gained a connection.autocommit) method set by default
to *false*.

Try this, instead:

connection = MySQLdb.connect(host="localhost", user="root", passwd="pw", 
db="japanese")

connection.autocommit(True)   # <--- note this
cursor = connection.cursor()

...other commands...

doing this you tell MySQL to automatically commit changes to db after
every UPDATE or INSERT.  turning autocommit to false is useful when
you wish to do changes in batches and maybe rollback the entire operation
if something went wrong (see commit() and rollback() methods for this).

hope this helps,
deelan.

-- 
"Però è bello sapere che, di questi tempi spietati, almeno
un mistero sopravvive: l'età di Afef Jnifen." -- dagospia.com



More information about the Python-list mailing list