ubuntu qt 源码编译 mysql驱动

ubuntu qt 源码编译 mysql驱动

1安装mysql

sudo apt-get install mysql-server
sudo apt-get install mysql-client

或者官网下载安装

2 安装dev

sudo apt-get install libmysqlcppconn-dev
sudo apt-get install libmysqlclient-dev

qt 源码编译

修改pro文件

TARGET = qsqlmysql

HEADERS += $$PWD/qsql_mysql_p.h
SOURCES += $$PWD/qsql_mysql.cpp $$PWD/main.cpp

#QMAKE_USE += mysql

OTHER_FILES += mysql.json

PLUGIN_CLASS_NAME = QMYSQLDriverPlugin

include(../qsqldriverbase.pri)


DESTDIR = $$_PRO_FILE_PWD_/target


INCLUDEPATH += /usr/include/mysql
LIBS += -L/usr/lib/mysql/ -lmysqlclient

编译命令

qmake "INCLUDEPATH+=/usr/include/mysql" "LIBS+= -lmysqlclient"
make 

然后target 目录生成两个文件

libqsqlmysql.so
libqsqlmysql.so.debug

复制到路径( Q T D I R / = 是 q t 安 装 目 录 ) QTDIR/=是qt安装目录) QTDIR/=qt)QTDIR/gcc_64/plugins/sqldrivers

测试

    qDebug()<<__FUNCTION__<<QSqlDatabase::drivers();

      QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
      db.setHostName("localhost");//127.0.0.1
      db.setPort(3306);
      db.setDatabaseName("testdb");
      db.setUserName("root");
      db.setPassword("wmx");
      bool ok = db.open();
      if (ok){
          QMessageBox::information(this, "infor", "success");
      }
      else {
          QMessageBox::information(this, "infor", "open failed");
          qDebug()<<"error open database because"<<db.lastError().text();
      }
}

你可能感兴趣的:(数据库,qt,mysql)