PDA

View Full Version : QSqlRecord & QSqlQuery problem with reading values



Rorsh
22nd September 2015, 22:12
Hello all,

I'm trying to read my SQLite database with Qt5 project.



QSqlQuery qry;
QString temp = "SELECT * FROM table WHERE id = 2";

qry.prepare(temp);

if(qry.exec())
{
QSqlRecord rec = qry.record();

for(int i=0; i<rec.count(); ++i) {
qDebug()<<rec.fieldName(i); //this part works, QDebug() prints out table column names, as it should
qDebug()<<rec.value(i).toString(); //this part doesn't work.. Where its "integer" field in the table I only get "0", and where its "TEXT" field in the table I only get ""
}
} else qDebug()<<qry.lastError().text();




As you can see, I'm having problems reading out values of QSqlRecord.

Additional info>
I'm sure my Query will only return one line, because I search by "id" field that is unique.

Does anybody know how I should read out column field values?

Added after 41 minutes:

Solved it:



QSqlQuery qry;
QString temp = "SELECT * FROM Testiranje2 WHERE id = "+ QString::number(curr);

qry.prepare(temp);

if(qry.exec())
{
while(qry.next()) {
int i=0;

QSqlRecord rec = qry.record();

for(int i=0; i<rec.count(); ++i) {
qDebug()<<rec.fieldName(i);
qDebug()<<qry.value(i).toString();
}
}
} else qDebug()<<qry.lastError().text();