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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -