mysql - Python => ValueError: unsupported format character 'Y' (0x59) -
i don't understand valueerror y. escape %...
table = town+"_history" db.execute("select date_format(snapdate,'%%y-%%m-%%d') date, sum( population ) accountpopulation, count( blockid ) number_block %s blockid =%%s group snapdate order snapdate desc limit 7" % table, (blockid))
you escape %%
use string formatter first:
"...." % table,
that returns new string %%
escaped percentages replaced single %
characters. mysql database adapter (ab)uses string formatting %
too, it'll take output , expect able fill %s
slots escaped sql literals. there '%y-%m-%d'
part of sql statement being interpreted again string format , error thrown.
the solution either double doubling:
db.execute("select date_format(snapdate,'%%%%y-%%%%m-%%%%d') date, sum( population ) accountpopulation, count( blockid ) number_block %s blockid = %%s group snapdate order snapdate desc limit 7" % table, (blockid,))
or use str.format()
instead , avoid having doubly-escape:
db.execute("select date_format(snapdate,'%%y-%%m-%%d') date, sum( population ) accountpopulation, count( blockid ) number_block {0} blockid = %s group snapdate order snapdate desc limit 7".format(table), (blockid,))
here {0}
replaced table name , %%
escapes left untouched; database adapter use %s
slot fill in blockid
parameter , return sql statement %%
escapes turned single %
characters.
Comments
Post a Comment