sqlite 插入整型时不支持的参数

elcex8rz  于 2022-11-15  发布在  SQLite
关注(0)|答案(3)|浏览(199)

我尝试将日期和时间存储在SQLite3中,以便稍后使用比较来检索记录,例如SELECT * WHERE date1 < date2。我放弃了存储datetime.datetime对象的尝试,并决定改用UNIX时间戳,因为它们只是一个int,但我仍然收到错误。

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.ecute(‘INSERT INTO NEW_TEST(Curent_Dt)VALUES(?)’,(Temp))ValueError:参数的类型不受支持

sqserrrh

sqserrrh1#

请注意在下面的“temp”之后添加的逗号:

cur.execute('insert into new_test (curent_dt) values (?)', (temp,))

发生这种情况的原因是,(temp)是一个整数,但(temp,)是一个长度为1的元组,其中包含temp

bweufnob

bweufnob2#

您的代码有错误:

temp = int(time.mktime(now.timetuple()))
cur.execute('insert into new_test (curent_dt) values (? )', temp)

您的代码没有错误:

temp = [(int(time.mktime(now.timetuple())))]
cur.execute('insert into new_test (curent_dt) values (? )', temp)

出现错误时的代码:

all_stats_changed = change_health, change_damage, change_defense, change_cooldown

c.executemany("INSERT INTO player_stats VALUES (?, ?, ?, ?)", all_stats_changed)

我的代码在工作时:

all_stats_changed = [(change_health, change_damage, change_defense, change_cooldown)]

c.executemany("INSERT INTO player_stats VALUES (?, ?, ?, ?)", all_stats_changed)

基本上,将代码放在[()]中就可以解决问题。

k7fdbhmy

k7fdbhmy3#

用下面的代码更改该行

cur.execute('insert into new_test (curent_dt) values (?)',str(temp))

相关问题