我对SQL和Sqlite3模块还很陌生,我想随机编辑数据库中所有记录的时间戳。
import sqlite3
from time import time
import random
conn = sqlite3.connect('database.db')
c = sqlite3.Cursor(conn)
ts_new = round(time())
ts_old = 1537828957
difference = ts_new - ts_old
for i in range(1,309):
#getting a new, random timestamp
new_ts = ts_old + random.randint(0, difference)
t = (new_ts, i)
c.executemany("UPDATE questions SET timestamp = (?) WHERE rowid = (?)", t)
#conn.commit()
运行时,我得到一个ValueError: parameters are of unsupported type
。
要最初添加timestamp
值,我将t
设置为一个元组,并将当前的UNIX时间戳设置为它的第一个值,例如(1537828957, )
。是否显示此错误,因为我使用了两个(?)
,而不是我在语句中添加开始时使用的单个时间戳?
答案 0 :(得分:1)
您使用的是executemany
而不是execute
。 executemany
采用元组的迭代器,并为每个元组执行查询。
您想使用execute
来代替,它将使用您的tuple
执行一次查询。
c.execute('UPDATE questions SET timestamp = (?) where rowid = (?)', t)