0

我正在尝试更新数据库条目上的时间戳,当我使用它时,我似乎无法让 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)'
4

1 回答 1

2

您正在构建这样的 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))
于 2013-01-31T21:06:16.413 回答