Python MySQLdb:mysql_exceptions.ProgrammingError:(1064)

时间:2017-01-05 03:29:15

标签: python mysql

我正在尝试从MySQL检索数据。不幸的是,我收到以下错误:

  

_mysql_exceptions.ProgrammingError:(1064,"您的SQL语法出错;请查看与您的MySQL服务器对应的手册   版本的正确语法使用附近' 1 = , - 5000,1))AND   (IF(3 = ,5000,3)))在第1行")

volumeMin和volumeMax我把它作为整数。实际上,它可以是浮动的。我该如何修复错误?

import MySQLdb
import MySQLdb.cursors

db = MySQLdb.connect(host='xxx.mysql.pythonanywhere-services.com',user='xxx',passwd='xxx',db='xxx$default',cursorclass=MySQLdb.cursors.DictCursor)
curs = db.cursor()
name ='G%'
volumeMin = 1
volumeMax = 3
#request args was initially want to retrieve data from external app
#but eventually found that the problem is in MySQL request
"""name = request.args['name']
volumeMin = request.args['volumeMin']
volumeMax = request.args['volumeMax']
"""

query0 = "SELECT * FROM KLSE WHERE Stock LIKE %s AND "
query2 = "(Volume_changes_pc BETWEEN (IF %s='_',-5000,%s)) AND (IF(%s='_',5000,%s)))"

query = query0+query2
input = name,volumeMin,volumeMin,volumeMax,volumeMax
print query
print input
print (query,(input))
curs.execute(query,(input))
a = curs.fetchall()

打印输出(查询,(输入))

('SELECT * FROM KLSE WHERE Stock LIKE %s AND (Volume_changes_pc BETWEEN (IF %s=_,-5000,%s)) AND (IF(%s=_,5000,%s))) AND MACD LIKE %s ', ('G%', 1, 1
, 3, 3, 'H'))

解决

query2 = "(Volume_changes_pc BETWEEN (IF (%s='_',-5000,%s)) AND (IF(%s='_',5000,%s)))"

错过了一个支架

0 个答案:

没有答案