How Do You Make The Python Msqldb Module Use ? In Stead Of %s For Query Parameters?
MySqlDb is a fantastic Python module -- but one part is incredibly annoying. Query parameters look like this cursor.execute('select * from Books where isbn=%s', (isbn,)) whereas e
Solution 1:
I found a lot of information out there about paramstyle
that seemed to imply it might be what you wanted, but according to this wiki you have to use the paramstyle
your library uses, and most of them do not allow you to change it:
paramstyle
is specific to the library you use, and informational - you have to use the one it uses. This is probably the most annoying part of this standard. (a few allow you to set differentparamstyles
, but this isn't standard behavior)
I found some posts that talked about MySQLdb allowing this, but apparently it doesn't as someone indicated it didn't work for them.
Solution 2:
I don't recommend doing this, but the simplest solution is to monkeypatch the Cursor
class:
from MySQLdb.cursors import Cursor
old_execute = Cursor.execute
def new_execute(self, query, args):
return old_execute(self, query.replace("?", "%s"), args)
Cursor.execute = new_execute
Post a Comment for "How Do You Make The Python Msqldb Module Use ? In Stead Of %s For Query Parameters?"