Build a .exe file from .py with SQLite database

2019-07-11 13:16发布

问题:

Program makes appointments and places them into schedule which are written in QsQLite database. The program runs from .py but I need it to be in .exe. I have used cx_Freeze to create an .exe file, but the program does not generate the SQLite database. So here is my setup file:

from cx_Freeze import setup, Executable
import os
import sys

os.environ['TCL_LIBRARY'] = r'C:\Program Files\Python35\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Program Files\Python35\tcl\tk8.6'


build_exe_options = {"packages": [
  'os','sys','sqlite3'], 'include_files': [os.path.join(sys.base_prefix, 'DLLs', 'sqlite3.dll'), 'main.py','util.py','data.db']}

setup(
    name = "Eclients",
    version = "0.1",
    options = {"build_exe": build_exe_options},
    executables = [Executable("main.py")]
)

But database can't be opened

So,how it can be solved?

回答1:

You are not including your SQLite database file in the include_files statement. See the documentation: http://cx-freeze.readthedocs.io/en/latest/faq.html#using-data-files

A better solution, however, would be to provide an option to create the missing database file when needed. This would allow the database's SCHEMA be defined within your script, and be kept consistent with your programme logic. If it needs populating with data, this might however be a less optimal solution.



回答2:

Solved this problem by copying the whole 'sqldrivers' folder from C:\Program Files\Python35\Lib\site-packages\PyQt5\pluginsto main.exe directory.