qt:QSqlQuery返回QVariant的向量

时间:2010-12-20 12:32:47

标签: c++ qt sqlite

我正在使用QSqlQuery类查询sqlite数据库,如下所示:

 QSqlQuery query("SELECT country FROM artist");
 while (query.next()) {
     QString country = query.value(0).toString();
     doSomething(country);
 }

有没有办法直接从QSqlLite类获取QVariant的向量?像这样:

 QSqlQuery query("SELECT country FROM artist");
 while (query.next()) {
     std::vector< QVariant > allFields;
     allFields.push_back( query.value(0) );
     allFields.push_back( query.value(1) );
     allFields.push_back( query.value(2) );
     doSomething(allFields);
 }

1 个答案:

答案 0 :(得分:3)

使用boundValues:

QList<QVariant> allFields = query.boundValues().values();

P.S:我不会在Qt中使用std-containers。 Qt容器有一个主要优势 - 隐式共享。我建议使用QList而不是std :: vector