sqlite3.OperationalError:没有这样的表:store

时间:2018-09-11 11:27:59

标签: 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("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())

1 个答案:

答案 0 :(得分:1)

您忘记调用create_table之前先调用insert方法。由于尚未调用create_table方法,因此insert方法会尝试将记录插入到不存在的表中。

解决方案只是在create_table之前调用insert方法,如下所示:

create_table()    # Add this line before the insert
insert("biscuits", 500, 20000)