Driver not loaded Driver not loaded
博客专区 > fudum 的博客 > 博客详情
Driver not loaded Driver not loaded
fudum 发表于3年前
Driver not loaded Driver not loaded
  • 发表于 3年前
  • 阅读 111
  • 收藏 0
  • 点赞 0
  • 评论 0
摘要: qt5.2.0 static qodbc QSqlDatabase

先阐述一下开发环境: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:

  1. QSqlDatabase db  =  QSqlDatabase :: addDatabase ( "QPSQL" ) ;
  2. db. setHostName ( "acidalia" ) ;
  3. db. setDatabaseName ( "customdb" ) ;
  4. db. setUserName ( "mojito" ) ;
  5. db. setPassword ( "J0a1m8" ) ;
  6. bool ok  = db. open ( ) ;
The above snippet creates a default database; if you need more DBs, add a second parameter to the addDatabase call with the internal name you want to identify it with.

2. get the database handle wherever you need it

  1. QSqlDatabase db  =  QSqlDatabase :: database ( ) ;

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.





标签: qt 5.2.0
共有 人打赏支持
fudum
粉丝 3
博文 9
码字总数 5242
×
fudum
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: