简体   繁体   中英

How to make an executable python tkinter file with a connection to a sqlite3 database

I have made a simple CRUD database program like a very simple inventory system. It is able to read from database, update the database, delete things from database and etc. by doing queries to the SQLite3 database. Now, I would like to make it executable so that I could share it to users for them to try out the program.

By now, I have tried pyinstaller with such code: pyinstaller --onefile --add-data "database.db:." inventory.py pyinstaller --onefile --add-data "database.db:." inventory.py

It successfully compiled the program. However, when I execute the executable file, comes an error in the terminal saying sqlite3.OperationalError: no such table: items_list and it immediately close the program.

Here is the code inside the program for me to connect to the sqlite3 database:

db = sqlite3.connect('database.db')
cursor = db.cursor()

and for me to display the items in the treeview, I used:

def update(rows):
    tree.delete(*tree.get_children())
    for i in rows:
        tree.insert('', 'end', value=i)
...
query = 'SELECT ItemName, CardNo, StockCount FROM items_list'
cursor.execute(query)
rows = cursor.fetchall()
update(rows)

Is there a way where I could combine the.py file with the.db file and make an executable single program? Thank you very much in advance for your help!

I have written similar Inventory type scripts that all work with a sqlite database. All variables aside. I used pyinstaller.exe --onefile --windowed as command. When using Windows 10, it puts in in the C:\...\dist directory. I copied the database to the same path as the .exe file(s) with all imported scripts used in the .exe file and it accesses the respective database accordingly. Also basing it on other variables like to install the respective programs. (I just did it to be sure I do not run into any issues regarding alpha testing.) Then also depending on when the code, updates the .db file accordingly. (This method worked for me when I encountered similar issues)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM