0

I'm learning sqlite3 with python, but I've been facing this error: "sqlite3.OperationalError: no such table: store". How do I get around this?

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())
asked Sep 11, 2018 at 11:27

1 Answer 1

1

You forgot to call the create_table method before calling insert. As you haven't called the the create_table method the insert method tries to insert a record to a non existing table.

The solution is simply to call the create_table method before insert as follows:

create_table() # Add this line before the insert
insert("biscuits", 500, 20000)
answered Sep 11, 2018 at 11:35
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.