Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

SQLite database with Qt



  • Hello,
    I have a problem with a Sqlite database in my Qt project: I created a file "database.db" with the application SQLite database Browser inside the root directory of my project but I can't operate on it succesfully. My code is this:

    @
    bool Database::InizDb(QString x="database.db"){
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(x);
    if (db.open()){
    cout <<"true";
    //return true;
    }
    else {
    cout << "false";
    //return false;
    }
    QSqlQuery query;
    query.exec("SELECT cognome FROM Profilo");
    cout << query.size();
    //...
    }
    @

    the printed is "true" and "-1". The problem is that it prints "true" also if i write a wrong database name, then the "-1" means that the query is wrong but it isn't!

    What can it be? Who can help me?

    Thank you very much

    [EDIT: code formatting, pleas wrap in @-tags, Volker]



  • To cite "QSqlQuery::size() ":http://doc.qt.nokia.com/4.7/qsqlquery.html#size:

    bq. Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes. [Emphasis by me]

    SQLite does not support reporting the number of rows in the result set.


Log in to reply