Menu

#339 List of strings as parameters are double-quoted

MySQLdb-1.2
open
nobody
None
1
2015-04-13
2015-04-13
No

When using list of strings as placeholders in cur.execute(), these get double-quoted (and thus never match/etc.):

scfc@tools-bastion-01:~$ dpkg-query -l python-mysqldb; python
Gewünscht=Unbekannt/Installieren/R=Entfernen/P=Vollständig Löschen/Halten
| Status=Nicht/Installiert/Config/U=Entpackt/halb konFiguriert/
         Halb installiert/Trigger erWartet/Trigger anhängig
|/ Fehler?=(kein)/R=Neuinstallation notwendig (Status, Fehler: GROSS=schlecht)
||/ Name                              Version               Architektur           Beschreibung
+++-=================================-=====================-=====================-=======================================================================
ii  python-mysqldb                    1.2.3-2ubuntu1        amd64                 Python interface to MySQL
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> MySQLdb.version_info
(1, 2, 3, 'final', 0)
>>> con = MySQLdb.connect(host='tools.labsdb', db='toollabs_p', read_default_file='~/replica.my.cnf')
>>> cur = con.cursor()
>>> cur.execute('SELECT * FROM users WHERE name = %s', ('single string', ))
0L
>>> repr(cur._executed)
'"SELECT * FROM users WHERE name = \'single string\'"'
>>> cur.execute('SELECT * FROM users WHERE name IN %s', (('array', 'of', 'strings'), ))
0L
>>> repr(cur._executed)
'\'SELECT * FROM users WHERE name IN ("\\\'array\\\'", "\\\'of\\\'", "\\\'strings\\\'")\''
>>> 
scfc@tools-bastion-01:~$

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.