列数字。QMYSQLResult::数据:超出范围的列。

时间:2021-11-09 16:40:47

Have database. Have query with unknown count of columns. Need to put all answer in QList

有数据库。具有列数未知的查询。需要把所有答案都放在QList里。

database = QSqlDatabase::addDatabase("QMYSQL");
...
QString sql = "Select * from test";
QSqlQuery query = QSqlQuery(database);
query.exec(sql);

QList<QStringList> retList;

Use .isValid() on value.

使用.isValid()值。

while (query.next()) {
int count = 0;
bool flagValues = true;
QStringList row;
while(flagValues)
{
    QVariant value = query.value(count);
    if(value.isValid() && !(count == memCount)   )
    {
        count++;
        row.append(value.toString());
    }
    else
    {
        flagValues = false;
    }
}
retList.append(row);

All is ok, but i have a messages (not error) like in every row. :

一切都很好,但是我在每一行都有一条消息(不是错误)。:

  QMYSQLResult::data: column 3 out of range

I do not want to use additional query (like information_schema) to know columns number.

我不希望使用附加查询(如information_schema)来知道列号。

1 个解决方案

#1


1  

Use query.record().count() to obtain the number of columns. Thus:

使用query.record().count()来获得列数。因此:

database = QSqlDatabase::addDatabase("QMYSQL");
...
QString sql = "Select * from test";
QSqlQuery query = QSqlQuery(database);
query.exec(sql);
const int memCount = query.record().count();
// query loop goes here

#1


1  

Use query.record().count() to obtain the number of columns. Thus:

使用query.record().count()来获得列数。因此:

database = QSqlDatabase::addDatabase("QMYSQL");
...
QString sql = "Select * from test";
QSqlQuery query = QSqlQuery(database);
query.exec(sql);
const int memCount = query.record().count();
// query loop goes here