【问题标题】:Inputting the current time into an SQLite database将当前时间输入 SQLite 数据库
【发布时间】:2021-07-13 16:05:27
【问题描述】:

我创建了一个面部识别程序,我想知道将当前时间输入到 sqlite 数据库中的最佳方法是什么。我目前已经设法更新了用户的出勤率,但我不太确定如何更新检测时间。下面是我为系统编写的一些代码。

student = getStudent(ID)
connection = sqlite3.connect(r"/Users/...../Documents/...../FaceBase.db")
time = datetime.datetime.now()
command = "UPDATE Class SET Attendance='Present' WHERE ID="+str(ID)
command = "UPDATE Class SET Time = GETDATE()"
cursor = connection.execute(command)
connection.commit()
connection.close()

我收到的错误是:

    cursor = connection.execute(command)
sqlite3.OperationalError: no such function: GETDATE

【问题讨论】:

  • @JonSG 我已经更新了问题,运行时出现错误
  • 哎呀,对。数据库函数可能是datetime('now')。我认为这是默认值,因此您也可以简单地执行datetime(),请参阅:sqlite.org/lang_datefunc.html 了解更多信息。再次注意参数化查询,并且您的第二次更新将更新所有行,因为没有 where 子句:-)
  • @JonSG 感谢您的回复!我已经设法解决了这个问题,并且能够获得当前的时间和日期!再次感谢。

标签: python sql python-3.x sqlite


【解决方案1】:

更新列时间的解决方案如下所示,并使用我们可以使用datatime() 的数据库函数。下面是命令。

command = "UPDATE Class SET Attendance='Present', Time = datetime() WHERE ID="+str(ID)

【讨论】:

    猜你喜欢
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    相关资源
    最近更新 更多