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 に答える