QtSql application not working on the deployed machine

自闭症网瘾萝莉.ら 提交于 2019-12-02 13:06:56

Right, so there are a couple of things here to be discussed before providing a solution, namely:

1) When you develop software on a Windows machine and it works like a charm, but not on the machines onto which you have deployed your software, the first thing to check is the dependencies. Are they all shipped properly?

How would I go around to check it? Well, that is the purpose of the freely available dependency walker. You have to run on the machine onto which you deployed the software; in other words after the deployment. Do not run it on the development machine if it works there. Good, this is about debugging, so let us get Qt specific...

2) You are using the QtSql module to handle the database interaction which is good in a Qt application. However, the main dynamic library, aka. QtSql4.dll is about the abstracted functionality. That is, all Qt code does not deal with the specific drivers itself.

You could ask that why is it done so? The reason is relatively straight-forward, since the QtSql module provides an abstract interface, it ought to be possible to change the database storage mechanism on the fly without rebuilding and redeploying your software. What if the end user decides to use another database one day? It would not be possible with out this.

Right, we are approaching a generic design principle here which is called Plugin Architecture. The Qt Project has the documentation here about it. Now, we are very close to the solution, so let us get one step further to the end.

3) This could have been the "too long; do not read" part: when you deploy your application, you will need to get your end users to have the plugins, too. That means, you need to ship both types of sqls in this context, the QtSql4.dll as well as the specific driver plugin. You are apparently missing the later. Therefore, prepare for that to be shipped. But what second, right? OK, so you seem to be currently using sqlite and Qt 4, so you will need to get the sqlite Qt 4 driver for that.

You can check the Qt SQL Database drivers here yourself:

SQL Database Drivers

Depending on which sqlite version you are using, you have two options:

QSQLITE2 SQLite version 2

QSQLITE SQLite version 3

Basically, this is how you would build the driver yourself if you have to do that:

cd %QTDIR%\src\plugins\sqldrivers\sqlite
qmake "INCLUDEPATH+=C:/SQLITE/INCLUDE" "LIBS+=C:/SQLITE/LIB/SQLITE3.LIB" sqlite.pro
nmake

Once you have that dll, which I am sure you already do if you have the program working on Windows already, copy qsqlite4.dll $QTDIR\plugins\sqldrivers into a sqldrivers directory beside your application, the executable. It will be picked up automatically then without any hassle by Qt unless you mess with the paths manually which you ought to not be doing.

4) That being said, this is completely generic an approach, so this will work for any SQL type if you replace sqlite with the one you need.

Nejat

You should also put qsqlite4.dll in a directory named sqldrivers alongside the release version of your application executable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!