工作代码片段-qsqlite

打开数据库:

    QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName(mDataName);
    if(!database.open()) {
       return false;
    }
    QSqlQuery *queryer = new QSqlQuery();

查询数据库:
    QString selectStr = "";
    for(
QList<QString>::iterator iterator = args.begin();iterator < args.end();iterator++)
    {
         selectStr += *iterator + ",";
    }
    selectStr.remove(selectStr.size() - 1, 1);
    QString queryStr = QObject::tr("select %1 from %2").arg(selectStr,table);
    bool flag = queryer->exec(queryStr);
    if(!flag) {
        std::cout << "数据库查询失败" << std::endl;
        return;
    }
    if(!queryer->first())
    {
        std::cout << "没有数据" << std::endl;
        return;
    }
    do{
        proxy->execut(queryer);

    }while(queryer->next());

关闭数据库:
    if(queryer != NULL)
    {
       delete queryer;
    }
    database.close();

添加数据库插件搜索路经:
    QApplication::addLibraryPath(QObject::tr("./plugins"));

添加编译选项:
QT       += core gui sql network

你可能感兴趣的:(工作,数据库,database,delete,iterator,NetWork)