【问题标题】:Database not being displayed after build构建后不显示数据库
【发布时间】:2024-04-29 02:05:03
【问题描述】:

我正在使用 cx_Freeze 来构建我的可执行文件。我使用 Pyqt4 和 QtSql 模块来显示我的数据库,问题是当通过 python 脚本运行时,显示数据库并且表工作正常,但是当我将它作为可执行文件运行时,表不能正常工作并且没有显示任何内容。作为脚本运行时: 作为可执行文件运行时: 有什么理由吗?这是 cx_Freeze 的一些错误吗?

这是我创建表格的代码:

        self.ProductModel = QtSql.QSqlRelationalTableModel()
        self.ProductModel.setTable("Product")
        self.ProductModel.setRelation(6, QtSql.QSqlRelation("ProductType","ProductTypeID","Type"))
        self.ProductModel.select()

        fields = ["Product ID", "Product Name", "In Stock", "Expiry Date", "Stock Alert", "Price", "Product Type"] 
        for count in range(len(fields)):
            self.ProductModel.setHeaderData(count, QtCore.Qt.Horizontal, fields[count])

        edit = QtSql.QSqlTableModel.OnManualSubmit 
        self.ProductModel.setEditStrategy(edit)

        self.ProductView = QtGui.QTableView()
        self.ProductView.setModel(self.ProductModel)
        self.ProductView.setSelectionMode(self.mode)
        self.ProductView.setItemDelegate(ProductDelegate())
        self.ProductView.sortByColumn(0,QtCore.Qt.AscendingOrder)
        self.ProductView.setSortingEnabled(True)

这应该没有什么问题,因为在脚本期间一切正常。

这是 cx_Freeze 的设置脚本代码:

from cx_Freeze import setup, Executable import sys import matplotlib

base = None if sys.platform == 'win32':
    base = 'Win32GUI'

executables = [Executable("Main.py", base = base)] includes = ['matplotlib', 'PyQt4.QtSql'] setup(name = 'Test',
      options = {"build_exe" : {"includes" : includes }},
      version = '0.18',
      description = 'Test',
      executables = executables)

【问题讨论】:

  • 我怀疑 QtSql 使用一些插件来与不同的数据库后端通信。如果插件没有在冻结过程中被复制,它将无法读取数据库。
  • @ThomasK 好吧,它确实连接并读取数据库正常,因为程序的另一部分使用 sql 查询对同一数据库工作正常,所以它不可能是连接问题

标签: python sqlite pyqt cx-freeze qsqltablemodel


【解决方案1】:

我在同一个问题上苦苦挣扎,但在另一个问题上(在 * 中)我发现解决方案是将文件夹 sqldrivers(我的位于 C:\Python34\Lib\site-packages\PyQt5\plugins)复制到您的可执行目录

【讨论】:

  • 另一个问题的链接是什么?确保将 URL 添加到答案中。
最近更新 更多