Reputation: 4341
I am trying to define my vector size with mysql database size in qt c++. After I execute my commands it returns 0 and get an error;
QMYSQLResult::data: column 1 out of range
in line:
int dbSize = query.value(1).toInt();
However when I execute sql command in mysql-workbench it says 15
Here is my code blocks for get database size :
int databaseConn::getDbSize()
{
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("SELECT count(*) FROM images");
query.exec();
query.next();
if( query.lastError().isValid())
{
qDebug() << query.lastError().text();
QSqlDatabase::database().rollback();
return -1;
}
else
{
int dbSize = query.value(1).toInt();
qDebug() << dbSize;
return dbSize;
}
}
Could you please help me where is my mistake?
PS: I dont want to call whole db and count it like :
QSqlQuery query("SELECT * FROM test");
query.record().count();
Upvotes: 1
Views: 653
Reputation: 4286
index
in QVariant QSqlQuery::value ( int index ) const
means zero-based index of field in the select statement. In your case, the only field count(*)
will have index 0:
int dbSize = query.value(0).toInt();
You can get fields' count from QSqlRecord
:
qDebug() << query.record().count();
Upvotes: 2