Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

python - parameter unsupported when inserting int in sqlite

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()

returns the following error:

cur.execute('insert into new_test (curent_dt) values (? )', (temp)) ValueError: parameters are of unsupported type

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,)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Note the added comma after "temp" below:

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

The reason this happens is that (temp) is an integer but (temp,) is a tuple of length one containing temp.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...