开发者

How to load drivers in qt?

I am using Ubuntu and I have Qt offline which is installed from an offline installer. While connecting to MySQL database I have got an error

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QSQLITE2
QSqlError(-1, "Driver not loaded", "Driver not loaded") 
Failed to connect.
The program has unexpectedly finished.

now I tried this code

#include <QCoreApplication>
#include <QSqlDatabase>
#include <QStringList>
#include <QtDebug>

int main( int argc, char **argv )
{
QCoreApplication app( argc, argv );
qDebug() << QSqlDatabase::drivers();
}

and got out开发者_开发知识库put

("QSQLITE", "QSQLITE2") 

then how to connect my Qt to MySQL database? I successfully connected to sqlite database.


in ubuntu there some configurations for offline installer.you can easily connect to mysql by installing qt from ubuntu repository.the important thing is that,the offline installer have 1gb size.but the packages downloaded from ubuntu repository have a size less than 200 mb.


You need to read this article. You might be better off to install Qt using an official Ubuntu package and there is probably a MySQL driver package available as well. I managed to install the Qt Oracle (QOCI) driver on my Fedora 15 system with a fair amount of work (I will elaborate if you want) as there wasn't a Qt Oracle package available under Fedora, but basically you are creating a shared object that is installed into $QTDIR/plugins/sqldrivers.


If you are Ubuntu linux OS, you can install library:

mic@ubt: ~$ apt-cache search libqt4-sql-mysql
libqt4-sql-mysql - Qt 4 MySQL database driver

mic@ubt: ~$ sudo apt-get install libqt4-sql-mysql 


Try reading this step by step article: http://www.pikopong.com/blog/2009/07/22/how-to-enable-mysql-support-in-qt-sdk-for-linux/

For an updated version of the article, you can read: http://www.pikopong.com/blog/2011/07/11/how-to-enable-mysql-support-in-qt-sdk-for-windows-part-2/

Even though the second article is for Windows, it should give you some idea on how to enable the driver.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜