【发布时间】:2019-02-23 15:56:24
【问题描述】:
我有日期列表
datefromto=['2018-06-16','2018-08-10','2018-09-12']
选择查询应该针对给定的日期进行筛选并存储在 csv 文件中
for dates in datefromto:
yesterbilling="""select * from student_date where date(datetime)= """+str(dates)+""" and daynumber="""+str(time.strftime("%w", time.strptime(dates, "%Y-%m-%d")))+""" and status='attended' """
cursor.execute(yesterbilling)
yesterbillings = cursor.fetchall()
myFile = open(students.csv, 'w')
with myFile:
writer = csv.writer(myFile)
writer.writerows(yesterbillings)
print("Writing complete")
这适用于没有 forloop 的单个日期。 for 循环中的 Mysql 不起作用。没有错误,但数据没有写入。我在 for 循环之外编写了 with 函数,但没有数据写入 csv。
请帮帮我
【问题讨论】:
-
我认为在这种情况下您不需要循环。难道你不能在日期列表中找到所有日期的位置吗?您还应该参数化查询以防止 SQL 注入等。
-
@DanielGale:在 sql 语句中,它们是从列表中的日期派生的天数(0 到 6)。这就是我尝试使用 for 循环的原因。你能帮忙吗
标签: python python-2.7 for-loop mysql-python