1 回答
TA贡献1833条经验 获得超4个赞
执行查询后,您需要获取结果:
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()
如果要添加筛选器,只需调整查询并添加参数:
query = "select bill_number from table where created_at between %s and %s and product=%s"
cursor.execute(query, (start_date, end_date, product))
为了使用列表作为参数,您可以使用和: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
>>>
确保您阅读了文档,因为它们包含许多有价值的信息。
添加回答
举报