我是新手。我做了一些搜索,但似乎无法找到这个问题的答案。我正在尝试使用Sqlite3和Python 3运行动态数据条目,但我收到错误。
它应该创建一个test1.db文件,但它是空白的。
错误:
Traceback (most recent call last):
File "/home/dominic/Documents/Database_Tutorial.py", line 35, in <module>
dynamic_data_entry()
File "/home/dominic/Documents/Database_Tutorial.py", line 29, in dynamic_data_entry
(unix, date, keyword, value))
sqlite3.OperationalError: no such table: stuffToPlot
Database_Tutorial.py
import sqlite3
import time
import datetime
import random
conn = sqlite3.connect('test1.db')
c = conn.cursor()
def create_table():
c.execute("CREATE TABLE IF NOT EXISTS stuffToPlot(unix REAL, datestamp TEXT, keyword TEXT, value REAL)")
def data_entry():
c.execute("INSERT INTO stuffToPlot VALUES(1452549219,'2016-01-11 13:53:39','Python',6)")
conn.commit()
c.close()
conn.close()
def dynamic_data_entry():
unix = int(time.time())
date = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%m-%d %H:%M:%S'))
keyword = 'Python'
value = random.randrange(0, 10)
c.execute("INSERT INTO stuffToPlot (unix, datestamp, keyword, value) VALUES (?, ?, ?, ?)",
(unix, date, keyword, value))
conn.commit()
for i in range(10):
dynamic_data_entry()
time.sleep(1)
c.close
conn.close()
请帮忙。
答案 0 :(得分:0)
你必须在进行数据输入之前调用create_table()。