python sql语句 字符型变量问题
ringa_lee
ringa_lee 2017-04-17 16:14:13
0
1
508

sql = "SELECT DISTINCT TradingDay FROM %s WHERE TradingDay <= %s ORDER BY TradingDay DESC"
cur.execute(sql, ("new_beta", '2010-01-05'))
报错
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''new_beta' WHERE TradingDay <= '2010-01-05' ORDER BY TradingDay DESC' at line 1")

应该是表明那地方用的"new_beta",%s进去的,还是有两个单引号,出现这种'new_beta' WHERE TradingDay <= '2010-01-05' ORDER BY TradingDay DESC,请问有什么办法去掉引号呢

ringa_lee
ringa_lee

ringa_lee

reply all(1)
阿神

Solved,

sql = "SELECT DISTINCT TradingDay FROM %s WHERE TradingDay <= %s ORDER BY TradingDay DESC" % (tablename,'%s')
cur.execute(sql, tradeday)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template