MySql教程

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连接MySQL代码示例 connectDB的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!