Skip to main content
 首页 » 编程设计

python之cx_freeze python sqlite3 数据库在 build.exe 后不起作用

2024年10月24日28jiqing9006

基本上我有一个使用 sqlite3 数据库的 pyqt 应用程序,现在我使用 Cx_Freeze 将它变成一个可执行文件。

我发现当数据库和查询作为 .py 运行时,它们运行完美,但在 cx_freeze 转换为 .exe 后,gui 运行完美,但数据库不响应任何查询。

这是设置脚本的代码:

from cx_Freeze import setup, Executable 
 
# Dependencies are automatically detected, but it might need 
# fine tuning. 
buildOptions = dict(packages = [], 
    excludes = [], 
    includes = ["sip", "PyQt5.QtSql"], 
    include_files = ["tpaData.db"]) 
 
import sys 
base = 'Win32GUI' if sys.platform=='win32' else None 
 
executables = [Executable('testTpa.py', base=base)] 
 
setup( 
    name='Tpa APP', 
    version = '0.1', 
    description = 'A PyQt TPA Program', 
    options = dict(build_exe = buildOptions), 
    executables = executables 
) 

这是我用来实例化数据库和应用程序的代码:

def __init__(self, dialog): 
        Ui_Form.__init__(self) 
        self.setupUi(dialog) 
        self.createConnection() 
 
def createConnection(self): 
        self.db = QtSql.QSqlDatabase.addDatabase('QSQLITE') 
        self.db.setDatabaseName("tpaData.db") 
        self.db.open() 
        self.query = QtSql.QSqlQuery() 
        self.query.exec_("create table doses(dose text, bolus text, discard text, remaining text, time1 text, time2 text, time3 text, comment text)") 

稍后在应用程序中,我使用 query.prepare 方法生成输入字符串,然后使用 query.bind 方法将值绑定(bind)到 query.prepare 字符串。最后,我使用 query.exec_() 提交准备好的字符串。

在开发环境(.py 文件)中工作,只是发布 cx_freeze 它失败了。

在此先感谢您的帮助。

请您参考如下方法:

cx_freeze 不会复制包含库数据库驱动程序的文件夹。例如,如果您将它们从“*C:\Python27\Lib\site-packages\PyQt4\plugins\sqldrivers*”复制到您的构建目录中的目录“sqldrivers”,应该可以。我有一个类似的案例,但使用 PySide - 它有效。