mysql - How can I prevent extra quotations being inserted into MySQLdb statement from Python? -


i have series of sql select statements need run python script using mysqldb. 1 of variables pass select statements called unit_ids. i've tried treating unit_ids string tuple of strings. initially, backslashes being inserted string. after looking around online, i've been able avoid backslashes, quotation marks being inserted instead. here current code:

connection = mysqldb.connect('localhost', 'root', '*****', 'test') cur = connection.cursor  unit_ids = ('0a1', '0a2', '0a3', '0a4') attr = 'sample'  cur.execute("""select count(*) test attribute = %s , unit_id in %r""", (a, tuple(unit_ids))) 

using cur._last_executed, can see actual sql statement performed was:

select count(*) test attribute = 'sample' , unit_id in ("'0a1'", "'0a2'", "'0a3'", "'0a4'") 

any ideas on need change in order have ('0a1', '0a2', '0a3', '0a4') remain unchanged in sql statement?

update: here's exact output when using %s:

>>> cn = mysqldb.connect('localhost', 'root', '***', '***') >>> c = cn.cursor() >>> unit_ids = ('0a1', '0a2', '0a3', '0a4') >>> = 'foo' >>> c.execute("""select count(*) model_test attribute = %s , unit_id in %s""", (a, unit_ids)) 1l >>> print(c._last_executed) select count(*) model_test attribute = 'foo' , unit_id in ("'0a1'", "'0a2'", "'0a3'", "'0a4'") 

at point, think may need create separate variables each element of unit_ids (eg unit_id1 = '0a1'). i'm using python 2.7.9 , mysql server 5.6, way.



update 2: @thebjorn solved it: version of mysqldb outdated. after upgrading, there no more quotation marks being inserted sql statement.

you don't need magic, regular mysql way:

connection = mysqldb.connect('localhost', 'root', '*****', 'test') cur = connection.cursor()  unit_ids = ('0a1', '0a2', '0a3', '0a4') attr = 'sample'  cur.execute("""select count(*) test attribute = %s , unit_id in %s""", (a, unit_ids)) 

the wrinkle can see if a not included , unit_ids had 1 item, tuple syntax trip up. if make unit_ids list syntax wouldn't quite awkward:

unit_ids = ('0a1',) cur.execute("select count(*) test unit_id in %s", (unit_ids,)) 

which when inlined becomes:

cur.execute("select count(*) test unit_id in %s", (('0a1',),)) 

vs. using lists (one parameter, parameter 1 item list):

cur.execute("select count(*) test unit_id in %s", [['0a1']]) 

you can use %s mysql parameters (a few others can used too, not %r -- isn't string interpolation).

update: must doing something differently me.. here output of cursor._last_executed

>>> cn = mysqldb.connect('server', 'user', 'password', 'database') >>> c = cn.cursor() >>> unit_ids = ('0a1', '0a2', '0a3', '0a4') >>> c.execute("select count(*) foo id in %s", (unit_ids,)) traceback (most recent call last):   file ... _mysql_exceptions.programmingerror: (1146, "table 'djangodevelop.foo' doesn't exist") >>> c._last_executed "select count(*) foo id in ('0a1','0a2','0a3','0a4')" >>> 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -