[英]how to form this mysql select query using python-2.7
我正在使用Python-2.7和mysql數據庫,希望通過以下兩個或兩個參數中的任何一個通過select語句從table student中檢索數據-
SELECT * FROM student_details where student_city='some_City' or student_stream='Science' or both
參數將傳遞給操作SELECT語句的函數。
任何形式的指導/幫助,使我理解如何編寫where子句部分。 我不想單獨查詢。
抱歉,如果我提出的問題不正確或重復了該問題。 提前致謝 :)
我嘗試了一下,最后對我有用
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)
這與上述建議有些相似。 謝謝大家的指導。
使用圍繞MySQL的python包裝器並使用execute()運行所需的任何內容。有關詳細信息,請單擊此鏈接
http://www.tutorialspoint.com/python/python_database_access.htm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.