Answer the question
In order to leave comments, you need to log in
Why isn't querying SQlite DB running in Qt 5?
Hello! I'm trying to execute two consecutive queries to the same database to different tables. The first request is executed, the second is not. I checked the second request directly through the database, it is executed, not through Qt. lastError does not throw any errors for the second request.
Here is the code that makes queries to the database:
QSqlQuery a_query;
QString stringQuery =QString("SELECT id FROM name_cars WHERE name='%1'").arg(nameCar);
a_query.prepare(stringQuery);
a_query.exec();
QSqlRecord rec = a_query.record();
int idCar=-1;
while (a_query.next())
{
idCar = a_query.value(rec.indexOf("id")).toInt();
}
stringQuery =QString("SELECT color FROM cars WHERE name=%1").arg(idCar);
a_query.prepare(stringQuery);
a_query.exec();
rec = a_query.record();
QList<int> idColors;
while (a_query.next())
{
int idColor = a_query.value(rec.indexOf("color")).toInt();
idColors.append(idColor);
}
Answer the question
In order to leave comments, you need to log in
It seems to me that all the same, the query should be different (or you need to change the structure of the database): are you sure that you need to compare name with id ?
If this is not the reason, then try this call:
stringQuery =QString("SELECT color FROM cars WHERE name=%1").arg(idCar);
a_query.exec(stringQuery);
prepare
it as intended.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question