T
T
tschin2016-11-02 17:28:27
Qt
tschin, 2016-11-02 17:28:27

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

2 answer(s)
A
Alexander, 2016-11-02
@xpert13

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);

You are still not using prepareit as intended.

T
tschin, 2016-11-03
@tschin Автор вопроса

Проблема была проста и позорна: я коннектился к той версии БД, в которой еще не заполнена эта таблица :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question