在sqlite中插入int时,参数不受支持 [英] parameter unsupported when inserting int in sqlite
问题描述
在SQLite3中存储日期和时间,我一直在四处走走,目的是通过比较来检索记录。 SELECT * WHERE date1< date2
我终于放弃了尝试存储datetime.datetime对象,并决定使用UNIX时间戳,因为它们只是一个int并且易于操作,但我仍然收到错误。
I have been going around and around with storing date and time in SQLite3 with the intention of retrieving the records using comparisons later e.g. SELECT * WHERE date1 < date2 I finally gave up trying to store datetime.datetime objects and decided to use a UNIX timestamp instead as they are just an int and easy to manipulate but I am still getting errors.
import sqlite3 as lite
import datetime
import time
conn = lite.connect('dispatcher.db')
cur = conn.cursor()
query = "create table if not exists new_test (curent_dt)"
cur.execute(query)
conn.commit()
now = datetime.datetime.now() - datetime.timedelta(minutes=60)
temp = int(time.mktime(now.timetuple()))
cur.execute('insert into new_test (curent_dt) values (? )', (temp))
conn.commit()
conn.close()
返回以下错误
cur.execute('insert into new_test (curent_dt) values (? )', (temp))
ValueError: parameters are of unsupported type
在进一步调查问题后,我发现您必须使用一个尾部的逗号来创建单个元素元组,例如(temp,)
After investigating the problem a little further I found that you have to use a trailing comma to create a single element tuple e.g. (temp,)
推荐答案
请注意下面的temp之后添加的逗号:
Note the added comma after "temp" below:
cur.execute('insert into new_test (curent_dt) values (?)', (temp,))
发生这种情况的原因是(temp)
是一个整数,但(temp,)
是一个长度为1的元组,包含 temp
。
The reason this happens is that (temp)
is an integer but (temp,)
is a tuple of length one containing temp
.
这篇关于在sqlite中插入int时,参数不受支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!