先阐述一下开发环境:windows7 + vs2012 + qt5.2.0 + c++。开发服务器端的程序。
发布程序的时候,把exe文件,dll文件,plugins文件下的驱动dll都copy到一个文件夹上。然后copy到服务器上,运行时候,日志文件一直输出Driver not loaded,不能够对数据库进行操作。
因为服务器上已经部署了一个程序,用相同的配置开发的,没有问题,很是纳闷,在网上搜索,找到一篇文章http://qt-project.org/forums/viewthread/5664。上面有个叔叔回复帖子:
If you declare the variable in the header as global, it is up to the compiler, when exactly the object is created (and that varies from compiler to compiler!), so it can be that there is an attempt to create the object at a time, when Qt is not finally set up yet – which can lead to not finding plugins and the like.
The steps are basically:
1. create a database connection somewhere in a connectDb method or the like:
-
db. setHostName ( "acidalia" ) ;
-
db. setDatabaseName ( "customdb" ) ;
-
db. setUserName ( "mojito" ) ;
-
db. setPassword ( "J0a1m8" ) ;
-
bool ok = db. open ( ) ;
2. get the database handle wherever you need it
This retrieves the previously setup default database; add a name to it if you have multiple ones.
3. close the database in a disconnectDB method
Get the handle like above and call close on it.
This way you get rid of the global object and hence avoid initialization hassles caused by the linker.
Oh, and an important note:
QSqlDatabase::addDatabase() [doc.qt.nokia.com], you must not call it on an QSqlDatabase object created with new! Just do it like the samples in the docs (= the snippets I copied above) show you; you just need to change the driver name, username, password, etc.
看到第一句话,If you declare the variable in the header as global.我就明白了,我的程序把QSqlDatabase db;定义成静态的了,有的时候在qt没有加载驱动的情况下加载。
我于是把程序改为:
在程序中先对数据库初始化:
//gps数据库连接
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC","one");
db.setDatabaseName(dbname);//数据库名
db.setUserName(usrname);//数据库用户名
db.setPassword(password);//数据库密码
db.setPort(prot);//设置数据库的端口
以后调用的时候利用:
QSqlDatabase db = QSqlDatabase::database("one",true);
来调用db连接,后来运行程序就解决了那个bug.