Qt 通过ODBC连接MySQL数据库

软件版本

  • Qt Creator 4.3.1
  • MySQL 5.7

连接MySQL

  1. 工程 .pro文件中添加QT += sql

  2. 头文件中添加如下内容:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include 
    using namespace std;
    
  3. 测试支持驱动类型

    void catDriver(){
        qDebug()<<"Available drivers:";
        QStringList drivers = QSqlDatabase::drivers();
        foreach(QString driver, drivers)
            qDebug() << driver;
    }
    
  4. 连接数据库

    bool connectionSql(){
        QSqlDatabase db=QSqlDatabase::addDatabase ("QODBC3");
           QString dsn = QString("DRIVER={MySQL ODBC 8.0 Unicode Driver};"
                                 "SERVER=%1;"
                                 "DATABASE=%2;"
                                 "UID=%3;"
                                 "PWD=%4;")
                   .arg("127.0.0.1")
                   .arg("bzd")
                   .arg("root")
                   .arg("mysql");
           db.setDatabaseName(dsn);
           if(!db.open ())
           {
               cout<<"DataBase Error :"<<endl;
               qDebug()<<db.lastError().text();
           }
           else
           {
               cout<<"success"<<endl;
               QSqlQuery  query=db.exec("SELECT * FROM `sys_user`");
               while(query.next())
               {
                   cout<<(query.value(0).toInt())<< "\t";
                   cout<<string((const char *)query.value(1).toString().toLocal8Bit())<<"\t";
                   cout<<(query.value(2).toInt())<< "\t";
                   cout<<query.value(3).toInt()<<endl;
               }
           }
    }
    
    
  5. 完整代码

    #include "mainwindow.h"
    #include 
    
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include 
    using namespace std;
    
    // 测试支持驱动类型
    void catDriver(){
        qDebug()<<"Available drivers:";
        QStringList drivers = QSqlDatabase::drivers();
        foreach(QString driver, drivers)
            qDebug() << driver;
    }
    
    // 连接数据库
    bool connectionSql(){
        QSqlDatabase db=QSqlDatabase::addDatabase ("ODBC");
           QString dsn = QString("DRIVER={MySQL ODBC 8.0 Unicode Driver};"
                                 "SERVER=%1;"
                                 "DATABASE=%2;"
                                 "UID=%3;"
                                 "PWD=%4;")
                   .arg("127.0.0.1")
                   .arg("bzd")
                   .arg("root")
                   .arg("mysql");
           db.setDatabaseName(dsn);
           if(!db.open ())
           {
               cout<<"DataBase Error :"<<endl;
               qDebug()<<db.lastError().text();
           }
           else
           {
               cout<<"success"<<endl;
               QSqlQuery  query=db.exec("SELECT * FROM `sys_user`");
               while(query.next())
               {
                   cout<<(query.value(0).toInt())<< "\t";
                   cout<<string((const char *)query.value(1).toString().toLocal8Bit())<<"\t";
                   cout<<(query.value(2).toInt())<< "\t";
                   cout<<query.value(3).toInt()<<endl;
               }
           }
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        
        catDriver();
        connectionSql();
    
        return a.exec();
    }
    
    

你可能感兴趣的:(Qt,MySQL,c++)