一步步学Qt,第四天-Linux 下mysql数据库链接
一步步学Qt,第四天-Linux 下mysql数据库链接
现在在linux系统下面,完成之前的qt-mysql连接工作,此次我新安装了新版的qt,也按照qtassistant对mysqldriver进行编译,但是没有得到驱动,不过还好,记得我之前有装过qt-mysql-driver
我的linux环境所Centos6.0 ,我的qt-mysql-driver所在目录是:
/usr/lib/qt4/plugins/sqldrivers
我的qtsdk安装目录是:
/home/power/QtSDK
那么将/usr/lib/qt4/plugins/sqldrivers下的libqsqlmysql.socp到qtsdk下面就好了:
命令:
[power@Power sqldrivers]$ pwd
/usr/lib/qt4/plugins/sqldrivers
[power@Power sqldrivers]$ ls
libqsqlite.so libqsqlmysql.so libqsqlodbc.so libqsqlpsql.so
[power@Power sqldrivers]$ cp libqsqlmysql.so ~/QtSDK/Desktop/Qt/473/gcc/plugins/sqldrivers/
现在开始写测试程序:
#include <QtGui> #include <QtSql> #include <QMessageBox> #include <QApplication> #include <QObject> bool createConnection(); int main(int argc,char *argv[]){ QApplication app(argc,argv); if(createConnection()){ QMessageBox::information(0,QObject::tr("showInfo"),QObject::tr("conn success!")); exit(0); } else{ QMessageBox::information(0,QObject::tr("showInfo"),QObject::tr("conn failure!")); exit(1); } return app.exec(); } bool createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setDatabaseName("mysql"); db.setUserName("root"); db.setPassword("admin"); if (!db.open()) { return false; } return true; }程序执行结果:
Starting /home/power/Workspaces/Qt/dbop1-build-desktop/dbop1...
/home/power/Workspaces/Qt/dbop1-build-desktop/dbop1 exited with code 0