Driver not loaded Driver not loaded

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





转载于:https://my.oschina.net/fudum/blog/283670

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值