Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python MySQLdb update query fails

Tags:

python

mysql

Okay. I've built here a mysql query browser, like navicat. Using MySQLdb to perform queries.

Here's the weird part. When i run the query through the program(using MySQLdb), it gives me success, affected rows = 1, but when i look at it in phpmyadmin, the value hasn't changed.

so before i perform the query, i print it out, copy and paste into phpmyadmin's query window, hit go and it works. So long story short, update query isn't working, but when i copy and paste into phpmyadmin, it works.

self.tbl.sql.use(self.tbl.database)       # switches to correct database. I've printed this and it uses the corrected db
if self.tbl.sql.execute(query) == True:
    print sql_obj.rows_affected()         # returns 1 (since i only do 1 query)

And here's the part of the SQL class

def execute(self, query):

    try:
        self.cursor.execute(query)
        return True
    except MySQLdb.ProgrammingError as error:
        print "---->SQL Error: %s" % error
        return False
    except MySQLdb.IntegrityError as e:
        print "--->SQL Error: %s" % e    
        return False

So any ideas what could be happening?

like image 555
sqram Avatar asked Jun 22 '09 18:06

sqram


People also ask

Does MySQLdb work with python3?

MySQLdb module, a popular interface with MySQL is not compatible with Python 3.

What is Python MySQLdb package?

MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2. 0 and is built on top of the MySQL C API. Packages to Install. mysql-connector-python mysql-python.

What is the difference between PyMySQL and MySQL?

PyMySQL and MySQLdb provide the same functionality - they are both database connectors. The difference is in the implementation where MySQLdb is a C extension and PyMySQL is pure Python. it might be easier to get running on some systems. it works with PyPy.


1 Answers

I believe @Jason Creighton and @S.Lott are correct.

At least if the table that you're updating is on a transactional storage engine. InnoDB is transactional, ISAM is not.

You either have to call commit() on your connection object before closing it, or you must set the connection to autocommit mode. I am not sure how you do that for a MySQLdb connection, I guess you either set an argument to the connection constructor, or set a property after creating the connection object.

Something like:

conn = mysql.connection(host, port, autocommit=True)

# or
conn = mysql.connection(host, port)
conn.autocommit(True)
like image 180
codeape Avatar answered Sep 22 '22 07:09

codeape