Qt6 topics merged with General

SQLITE no query unable to fetch the row

  • Hi everybody! I have a code'structure like this :

    I save data on a db in sqlite ahd it work fine for the file login and paginaprincipale, but when i do the same for the sc_assistivo file i get the error no quey unable to fetch the row. And i did the same identical things for all the three files.
    Does anybody have an idea?
    i works with cmake, but the file cmakelist seems to be correct. Moreover when i check if the db is connected i've got positive answer. the trouble starts when i try to modify the db after clicking a push putton (which is declared as a private slot)
    ps. i don't add the database path every time, i just have done it once and then in other files i use
    QSqlDatabase mydb2 = QSqlDatabase::database();

    QSqlDatabase mydb3 = QSqlDatabase::database();

  • Lifetime Qt Champion

    Please show some code - we can't do anything with the filenames of your project...

  • Lifetime Qt Champion


    If you only have one database and use the default connection, you don't need to do that.

  • Screenshot from 2020-05-30 21-41-07.png
    Screenshot from 2020-05-30 21-41-44.png

  • Lifetime Qt Champion

    As stated in the QSqlDatabase documentation, don't keep class member variable of that type.

    Beside that, in the constructor, you are shadowing that variable so the member variable isn't valid and if you use it later on, you'll be trying to access an invalid connection.

    Therefore: start by removing these QSqlDatabase member variables.

  • @SGaist sorry, i can't understand, which variable are you referring to? i try to remove QSqlDatabase member but i get that the db is not connected.

  • Lifetime Qt Champion

    Yes the one in the .h file.
    You dont use it in the code show as you have a local variable of same name.

    Also in the code shown, i cant see where you tell it what data file to use ?

  • i remove that but the i get the same error, i've created a connection with the database in another file, and then i use the same connection in this, so i don't need to use any path. the strange thing is that i also create a query to select name and last name in the database and shot in my window, and that works! but then, when i move into the check button function i can't do anything with db. so why in the ui(new Ui::sc:assistivo) i can read data and in the void function no? thank you all for taking the time

  • Lifetime Qt Champion


    Ok, its hard to overview with not all code but can we just note the following rules.

    1: dont store the database variable. Qt always keeps track of this for you.
    So its perfectly fine to open it somewhere and then uses query some where else.

    Only exception is if you want to use multiple databases at same file. As in different files.
    Then we need to use names pr database and give to query.

    2: If Sqllite dont find the file you ask it to open, It creates a new blank one and opens it
    so it looks good but it has no data.

    3: you must tell it a file or memory

    small example just make sure its clear.

    bool createConnection()
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        if (!db.open()) {
            QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.",
            return false;
        QSqlQuery query;
        qDebug() << "table:" <<   query.exec("create table person (id int primary key, "
                                             "firstname varchar(20), lastname varchar(20), num int )");
        query.exec("insert into person (firstname , lastname, num) values('Dennis', 'Young','1')");
        query.exec("insert into person values(102, 'Christine', 'Holand','2')");
        query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')");
        query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')");
        query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')");
        return true;
    // here we dont use the QSqlDatabase db  variable at all. we just use query
    void queryToCsv()
        QSqlQuery query;
        query.prepare(" select * from person;");
        QFile csvFile ("output.csv");
        if (!csvFile.open(QFile::WriteOnly | QFile::Text)) {
            qDebug("failed to open csv file");
        if (!query.exec()) {
            qDebug("failed to run query");
        QTextStream outStream(&csvFile);
        while (query.next()) {
            const QSqlRecord record = query.record();
            for (int i = 0, recCount = record.count() ; i < recCount ; ++i) {
                if (i > 0) {
                    outStream << ',';
                outStream << escapedCSV(record.value(i).toString());
            outStream << '\n';

  • thank you all!

Log in to reply