数据库
首页 > 数据库> > Qt连接MySQL代码示例 connectDB

Qt连接MySQL代码示例 connectDB

作者:互联网

void Widget::connectDB(){
    // ---> 读取配置文件
    QSettings * cfg = new QSettings(cfgpath,QSettings::IniFormat);
    int port = cfg->value("NET/port").toInt();
    QString dbhost = cfg->value("NET/ipad").toString();
    QString dbusr = cfg->value("NET/user").toString();
    QString dbpass = cfg->value("NET/pass").toString();
#ifdef DEBUG
    cout << "ip地址: " << dbhost;
    cout << "db端口: " << port;
    cout << "用户名: " << dbusr;
    cout << "密码: " << dbpass;
#endif
    cfg->disconnect();
    delete cfg;

    dbc = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL","prt"));
    dbc->setHostName(dbhost);
    dbc->setPort(port);
    dbc->setUserName(dbusr);
    dbc->setPassword(dbpass);
    dbc->setDatabaseName("nlp_data");

    if(!dbc->open()){
#ifdef DEBUG
        cout << "SQL执行失败!";
        cout << _sqlc;
        cout << dbc->lastError();
#endif
    } else {
        qyb = new QSqlQuery(*dbc);
    }
}


标签:Qt,示例,cfg,dbc,value,QString,MySQL,NET,port
来源: https://www.cnblogs.com/mc-r/p/15848610.html