好吧,我通常是sqlite和python的新手,所以请保持友好=) 我有一本简单的字典-
time = data[0]['timestamp']
price = data[0]['price']
myprice = {'Date':time,'price':price}
myprice看起来像这样(时间是时间戳)-
{'Date': 1553549093, 'price': 1.7686}
我现在想将数据添加到sqlite3数据库...所以我创建了这个-
#Create database if not exist...
db_filename = 'mydb_test.db'
connection = sqlite3.connect(db_filename)
#Get a SQL cursor to be able to execute SQL commands...
cursor = connection.cursor()
#Create table
sql = '''CREATE TABLE IF NOT EXISTS TEST
(PID INTEGER PRIMARY KEY AUTOINCREMENT,
DATE TIMESTAMP,
PRICE FLOAT)'''
#Now lets execute the above SQL
cursor.execute(sql)
#Insert data in sql
sql2 = ("INSERT INTO GBPCAD VALUES (?,?)", [(myprice['Date'],myprice['price'])])
cursor.execute(sql2)
cursor.commit()
connection.close()
但是执行此代码时,我得到ValueError: operation parameter must be str
我在做什么错了?
答案 0 :(得分:1)
在insert
中传递execute()
语句的参数:
sql2 = "INSERT INTO GBPCAD (DATE, PRICE) VALUES (?,?)"
cursor.execute(sql2, (myprice['Date'], myprice['price']))
还要在插入语句中包含列的名称。