首页 > 解决方案 > 在两个条件之间返回多于一列

问题描述

我有一个包含三列的 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)]。有关如何进行此计算的任何帮助或建议都会有所帮助。谢谢!

标签: pythonsqlite

解决方案


这些条件:

theDate>=:starting<=:endof

和:

theDate >=? <=?

不是有效的 SQL 条件。
你需要BETWEEN

theDate BETWEEN :starting AND :endof

或者:

theDate BETWEEN ? AND ?

推荐阅读