1 回答

TA貢獻1833條經(jīng)驗 獲得超4個贊
執(zhí)行查詢后,您需要獲取結(jié)果:
records = cursor.fetchall()
不要用于SQL查詢非常重要,因為它容易受到SQL注入攻擊;而是使用:format
query = "select bill_number from table where created_at between %s and %s"
cursor.execute(query, (start_date, end_date))
records = cursor.fetchall()
如果要添加篩選器,只需調(diào)整查詢并添加參數(shù):
query = "select bill_number from table where created_at between %s and %s and product=%s"
cursor.execute(query, (start_date, end_date, product))
為了使用列表作為參數(shù),您可以使用和:INtuple
>>> query = "select * from clients where added between %s and %s and score in %s"
>>> data = ('2019-01-01', '2020-03-01', tuple([1,2,3]))
>>> cursor.execute(query, data)
>>> rows = cursor.fetchall()
>>> len(rows)
32
>>>
確保您閱讀了文檔,因為它們包含許多有價值的信息。
添加回答
舉報