Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python mySQL Update, Working but not updating table

I have a python script which needs to update a mysql database, I have so far:

dbb = MySQLdb.connect(host="localhost",         user="user",         passwd="pass",         db="database")  try:    curb = dbb.cursor()    curb.execute ("UPDATE RadioGroups SET CurrentState=1 WHERE RadioID=11")    print "Row(s) were updated :" +  str(curb.rowcount)    curb.close() except MySQLdb.Error, e:    print "query failed<br/>"    print e   

The script prints Row(s) were updated : with the correct number of rows which have a RadioID of 11. If I change the RadioID to another number not present in the table it will say Row(s) were updated :0. However the database doesn't actually update. The CurrentState field just stays the same. If I copy and past the SQL statement in to PHPMyAdmin it works fine.

like image 268
user2144306 Avatar asked Mar 07 '13 13:03

user2144306


People also ask

Why UPDATE is not working in MySQL workbench?

You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.

How do you UPDATE a table in MySQL workbench?

You can add or modify the columns or indexes of a table, change the engine, add foreign keys, or alter the table name. To access the MySQL Table Editor, right-click a table name in the Navigator area of the sidebar with the Schemas secondary tab selected and click Alter Table.


2 Answers

use

dbb.commit() 

after

curb.execute ("UPDATE RadioGroups SET CurrentState=1 WHERE RadioID=11")

to commit all the changes that you 'loaded' into the mysql server

like image 58
Lazykiddy Avatar answered Oct 03 '22 02:10

Lazykiddy


As the @Lazykiddy pointed out, you have to commit your changes after you load them into the mysql.

You could also use this approach to enable the auto commit setting, just after the MySQL connection initialization:

dbb.autocommit(True) 

Then, it will automatically commit the changes you made during your code execution.

like image 43
Cyclone Avatar answered Oct 03 '22 02:10

Cyclone