我正在使用Python-2.7和mysql数据库,希望通过传递2个或2个参数中的任何一个参数,使用select语句从表student中检索数据,如下所示-
SELECT * FROM student_details where student_city='some_City' or student_stream='Science' or both
参数将被传递给操作SELECT语句的函数。
任何形式的指导/帮助,使我了解如何编写where子句部分。我不想做单独的查询。
如果我问错了或重复了问题,我深表歉意。提前感谢:)
发布于 2016-02-02 21:21:00
我试过了,最后这个方法对我有效-
try:
query = ("""SELECT * FROM student_details WHERE """)
if city is not '0' and Stream == '0':
complete_query = query+("""student_city = %s""")
elif Stream is not '0' and city == '0':
complete_query = query+("""student_stream = %s""")
else:
complete_query = query+("""student_city = %s AND student_stream = %s""")
if city is not '0' and Stream == '0':
s_execute = self.cur2.execute(complete_query,(city,))
elif Stream is not '0' and city == '0':
s_execute = self.cur2.execute(complete_query,(Stream,))
else:
s_execute = self.cur2.execute(complete_query,(city),(Stream))
except MySQLdb.Error as error:
print(error)这是一些与上述建议类似的建议。感谢大家的指导。
发布于 2016-01-22 17:03:53
在MySQL周围使用python包装器,并使用execute()来运行您的want.For详细信息,请单击此链接
http://www.tutorialspoint.com/python/python_database_access.htm
发布于 2016-01-22 19:00:51
https://stackoverflow.com/questions/34941475
复制相似问题