python - 在两个条件之间返回多于一列
问题描述
我有一个包含三列的 SQLite 数据库;EmpName、theTime 和 theDate。我正在尝试运行一个查询,该查询将返回符合特定日期范围的 EmpName 和 theTime 行。我的目标是将所有这些时间加起来并给出一个周末的总时间。但是,当我运行我的代码时,它会返回一个 0 列表。
我尝试过以下查询:
SELECT * FROM Swipes WHERE theDate>=:starting<=:endof", {'starting': fstartingDate, 'endof': formatted}
和
SELECT EmpName AND theTime FROM Swipes WHERE theDate >=? <=?, (fstartingdate, formatted,)
和
SELECT EmpName AND theTime FROM Swipes WHERE thedate BETWEEN ? AND ?", (fstartingDate, formatted)
以及该方法的其他变体,但感觉就像我在兜圈子。这是我的代码:
def weekSummary(endofthatweek):
formatted = dt.strptime(endofthatweek, '%Y-%m-%d')
startingDate = formatted - td(days=7)
fstartingDate = startingDate.date()
con = sqlite3.connect(r'C:\Users\zstrickland.RANDSMACHINE\Documents\PymeClock\testTimeclock.db')
cur = con.cursor()
cur.execute("SELECT EmpName AND theTime FROM Swipes WHERE theDate>=:starting<=:endof", {'starting': fstartingDate, 'endof': formatted})
tupes = cur.fetchall()
con.close()
detuped = [x[0] for x in tupes]
print(detuped)
我希望得到一个列表(可能是元组列表..),格式如下:[(EmpName, theTime), (EmpName, theTime), (EmpName, theTime), (EmpName, theTime)]。有关如何进行此计算的任何帮助或建议都会有所帮助。谢谢!
解决方案
这些条件:
theDate>=:starting<=:endof
和:
theDate >=? <=?
不是有效的 SQL 条件。
你需要BETWEEN
:
theDate BETWEEN :starting AND :endof
或者:
theDate BETWEEN ? AND ?
推荐阅读
- google-chrome - 是否有某个页面我应该从我的 Chrome 扩展项目中获取我的 API 数据?
- mysql - 如何在 MYSQL 中编写“当匹配然后更新”
- mongodb - 如何在几分钟内将 700+ 百万行导入 MongoDB
- ios - 从 UITabBarController 选项卡根视图呈现 UINavigationController
- c# - 我可以注册特定域以在应用程序中运行吗?
- angular - Angular 对话框的奇怪行为
- php - 具有多个连接的重复行
- jquery - jQuery:点圆设置位置菜单悬停/悬停并单击导航链接
- java - 如何将参数从java类传递给build.xml?
- java - Excel 创建一个在 Excel 列中生成值 1 的 IF 语句