Search code examples
pythonsqlitecx-freezeqsqldatabase

cx_freeze python sqlite3 database doesn't work after build.exe?


Basically i have a pyqt application that uses a sqlite3 database, now I used Cx_Freeze to turn it into an executable.

I find that the database and queries run perfectly when they are ran as a .py, but after the cx_freeze conversion to .exe the gui works flawlessly but the database doesn't respond to any queries.

Here is the code for the setup script:

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
)

here is the code I use to instantiate the db and app:

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)")

later in the app i use the query.prepare method to make a string of the inputs and then query.bind method to bind values to the query.prepare string. Finally i use query.exec_() to submit that prepared string.

In the dev environment(.py file) works, just post cx_freeze it fails.

Thanks in advance for the help.


Solution

  • cx_freeze does not copy the folder with the drivers for the library databases. If you copy them for example from "*C:\Python27\Lib\site-packages\PyQt4\plugins\sqldrivers*" with the directory "sqldrivers" in your build directory should work. I have a similar case, but with PySide - it worked.