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);
    }
}


上一篇:防止xss攻击,过滤参数


下一篇:luogu P2678 跳石头 二分答案