您好我正在开发一个项目,我正在尝试从SQLite表中选择数据并将其显示在一个简单的网页上。我正在尝试显示本周的事件,当前的事件显示在页面顶部,然后是明天的事件等。我正在尝试选择我的SQLite表中与日期匹配的事件,但是当我加载网页时,它是空白的。没有事件显示(如果我只是SELECT * FROM
表中所有事件都在那里,所以我知道错误与我的SQL查询有关。有人可以解释一下我做错了吗?
如果有帮助,变量date1
到date7
都返回一个像YYYY-MM-DD
这样的字符串,用于从SQLite表中选择与此日期匹配的事件。我必须使用LIKE,因为在表<date>
中存储为YYYY-MM-DD 00:00:00
,但我不关心只有一天的时间。然后我将<rows>
传递给<events>
,这是我在HTML文件中使用的变量。非常感谢你!
# 1st day
date1 = str((datetime.datetime.now() + datetime.timedelta(days=0)).date())
rows1 = db.execute ("SELECT * FROM events WHERE date LIKE :date ORDER BY date", date = date1)
# 2nd day
date2 = str((datetime.datetime.now() + datetime.timedelta(days=1)).date())
rows2 = db.execute ("SELECT * FROM events WHERE date LIKE :date ORDER BY date", date = date2)
# 3rd day
date3 = str((datetime.datetime.now() + datetime.timedelta(days=2)).date())
rows3 = db.execute ("SELECT * FROM events WHERE date LIKE :date ORDER BY date", date = date3)
# 4th day
date4 = str((datetime.datetime.now() + datetime.timedelta(days=3)).date())
rows4 = db.execute ("SELECT * FROM events WHERE date LIKE :date ORDER BY date", date = date4)
# 5th day
date5 = str((datetime.datetime.now() + datetime.timedelta(days=4)).date())
rows5 = db.execute ("SELECT * FROM events WHERE date LIKE :date ORDER BY date", date = date5)
# 6th day
date6 = str((datetime.datetime.now() + datetime.timedelta(days=5)).date())
rows6 = db.execute ("SELECT * FROM events WHERE date LIKE :date ORDER BY date", date = date6)
# 7th day
date7 = str((datetime.datetime.now() + datetime.timedelta(days=6)).date())
rows7 = db.execute ("SELECT * FROM events WHERE date LIKE :date ORDER BY date", date = date7)
return render_template("index.html", events1 = rows1, events2 = rows2, events3 = rows3, evetns4 = rows4, events5 = rows5, events6 = rows6, events7 = rows7)
答案 0 :(得分:0)
变化:
rows1 = db.execute ("SELECT * FROM events WHERE date LIKE :date ORDER BY date", date = date1)
to(添加日期(日期)以删除时间部分。)
rows1 = db.execute ("SELECT * FROM events WHERE date(date) =:date ORDER BY date", date = date1)