sqlite 如何使用APSW更新字段

p4rjhz4m  于 2022-12-27  发布在  SQLite
关注(0)|答案(1)|浏览(197)

我尝试在使用数据库条目时更新该条目上的时间戳,但似乎无法使Update语句工作

import apsw
    from datetime import datetime

    def updateTimestamp(primary, timestamp):
        tableName = 'Images'

        connection = apsw.Connection('Images')
        cursor = connection.cursor()

        sql = "UPDATE %s SET timestamp = %s WHERE id = %s" %(tableName, timestamp, primary)
        print "ok so far"
        cursor.execute(sql)

        if connection:
            connection.close()

    currentTime = datetime.now()
    #remove spaces from timestamp
    currentTime = str(currentTime).replace(' ', '')
    updateTimestamp(1, currentTime)

我正在使用apsw尝试更新字段,但它不起作用,我得到错误

"apsw.SQLError: SQLError: near ":05": syntax error"

我的表看起来像:

sql = 'CREATE TABLE IF NOT EXISTS ' + tableName + '(id INTEGER PRIMARY KEY 
    AUTOINCREMENT, Name TEXT, Metadata TEXT, Mods TEXT, Certification TEXT, 
    Product TEXT, Email TEXT, notes TEXT, timestamp TEXT, ftpLink TEXT)'
0kjbasz6

0kjbasz61#

您正在构建一个如下所示的SQL命令:

UPDATE Images SET timestamp = 2013-01-3121:59:00.427408 WHERE id = 1

正确的语法应该如下所示:

UPDATE Images SET timestamp = '2013-01-31 21:59:00.427408' WHERE id = 1

但是,要避免字符串格式问题,应使用参数:

sql = "UPDATE %s SET timestamp = ? WHERE id = ?" % (tableName)
cursor.execute(sql, (timestamp, primary))

相关问题