Python sqlite3.0错误:没有这样的表:存储
我正在用python学习sqlite3,但我一直面临这个错误:sqlite3.OperationalError:没有这样的表:store。我该怎么做Python sqlite3.0错误:没有这样的表:存储,python,sqlite,Python,Sqlite,我正在用python学习sqlite3,但我一直面临这个错误:sqlite3.OperationalError:没有这样的表:store。我该怎么做 import sqlite3 def create_table(): #function to create the table conn = sqlite3.connect('lite.db') cur = conn.cursor() # creating th cursor object cur.execute("CR
import sqlite3
def create_table(): #function to create the table
conn = sqlite3.connect('lite.db')
cur = conn.cursor() # creating th cursor object
cur.execute("CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price REAL)")
conn.commit()
conn.close()
def insert(item, quantity, price ): #function to insert into the table
conn = sqlite3.connect('lite.db')
cur = conn.cursor() # creating th cursor object
cur.execute("INSERT INTO store VALUES(?,?,?)", (item, quantity, price))
conn.commit()
conn.close()
insert("biscuits",500,20000)
def view():
conn = sqlite3.connect('lite.db')
cur = conn.cursor()
cur.execute("SELECT * FROM store")
rows = cur.fetchall()
return rows
conn.close()
print(view())
在调用insert之前,您忘记调用create_table方法。由于您尚未调用create_table方法,insert方法尝试将记录插入到不存在的表中
解决方案只是在插入之前调用create_table方法,如下所示:
create_table() # Add this line before the insert
insert("biscuits", 500, 20000)