如何用APSW更新字段
我正在尝试在使用数据库条目时更新时间戳,但我似乎无法让更新语句正常工作。
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)'
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))