0

I have made a database with a table called product. When I run the code below I get an error - sqlite3.OperationalError: no such table: product. I have used a database browser to check and the table does exist. Any ideas? The code and the file are both in the same folder. Thanks

from tkinter import *
from tkinter import ttk
import sqlite3
import os.path

class Product:
    db_name = 'database.db'

    def __init__(self, wind):
        self.wind = wind
        self.wind.title('IT Products')

        frame = LabelFrame (self.wind, text = 'Add new record')
        frame.grid (row = 0, column = 1)

        Label (frame, text = 'Name: ').grid (row = 1, column = 1)
        self.name = Entry (frame)
        self.name.grid(row = 1, column = 2)

        Label (frame, text = 'Price: ').grid (row = 2, column = 1)
        self.price = Entry (frame)
        self.price.grid(row = 2, column = 2)

        ttk.Button (frame, text= 'Add record').grid (row = 3, column =2 )
        self.message = Label (text = '',fg = 'red')
        self.message.grid (row = 3, column = 0)

        self.tree = ttk.Treeview (height = 10, colum =2)
        self.tree.grid(row = 4, column = 0, columnspan = 2)
        self.tree.heading('#0', text = 'Name', anchor = W)
        self.tree.heading(2, text = 'Price', anchor = W)

        ttk.Button (text = 'Delete record').grid (row = 5, column = 0)
        ttk.Button (text = 'Edit record').grid (row = 5, column = 1)

        self.viewing_records ()



    def run_query (self, query, parameters = ()): # database connection
        with sqlite3.connect(self.db_name) as conn:
            cursor = conn.cursor()
            query_result = cursor.execute (query, parameters)
            conn.commit()
        return query_result


    def viewing_records(self):
        records = self.tree.get_children()
        for element in records:
            self.tree.delete (element)
        query = 'SELECT * FROM product ORDER BY name DESC'
        db_rows = self.run_query (query)
        for row in db_rows:
            self.tree.insert ('', 0, text = row[1], values = row[2])

if __name__ == '__main__':
    wind = Tk()
    application = Product (wind)
    wind.mainloop()

2 Answers 2

4
...
db_path = os.path.join(BASE_DIR, "database.db")
with sqlite3.connect('db_path') as conn:
...

You are creating a new database file with the name 'db_path'.

Instead of

with sqlite3.connect('db_path') as conn:

you should have

with sqlite3.connect(db_path) as conn:

Or in other words, use the variable db_path and not the literal string 'db_path'.

For the sake of completeness, you may also want to use Product.db_name that you have already defined instead of hard-coding 'database.db' again:

...
db_path = os.path.join(BASE_DIR, self.db_name)
with sqlite3.connect(db_path) as conn:
...
Sign up to request clarification or add additional context in comments.

3 Comments

updated the code and removed that bit. still got the same issue :( thanks for the help
@maz86 Pay close attention to the code in the bottom of my answer. You are still passing the wrong value to sqlite3.connect
just tried and same error. BASE_DIR = os.path.dirname(os.path.abspath(file)) db_path = os.path.join(BASE_DIR, self.db_name) with sqlite3.connect(db_path) as conn:
1

This creates a variable called db_path which contains the path to the file database.db

db_path = os.path.join(BASE_DIR, "database.db")

But this uses the literal string db_path as the name of the database to connect to, not the contents of the variable you've just created:

with sqlite3.connect('db_path') as conn:

To use the variable, remove the quotes:

with sqlite3.connect(db_path) as conn:

7 Comments

forgot to take those out! but still no luck
was only added to ensure it was checking the correct database Taken it out but no luckBASE_DIR = os.path.dirname(os.path.abspath(file)) db_path = os.path.join(BASE_DIR, "database.db")
@maz86 If you have modified your code and still get an error (same error or another one), please update the question with the latest code you have and the error you get.
Just updated. Thank you for your help. getting same error
@maz86 Pay close attention to the code in the bottom of my answer. You are still passing the wrong value to sqlite3.connect.
|

Your Answer

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.