Important: Please read the Qt Code of Conduct -

Load specific sqlite column and row into an integer variable

  • Im doing a qt application, where I need to load a specific SQLite value into a specific integer variable. There doesn't seem to be any simple solution on the net.

    My code is:
    QString table = "Measurement";
    QString column = "MeasurementId";
    QString data = "10";

    ptr_mydb = QSqlDatabase::addDatabase("QSQLITE");
    ptr_mydb.setDatabaseName("/home/stud/sqlite/ghdb.sqlite"); // database path on devkit;
        qDebug() << "Database connection established";
        qDebug() << "Database connection error";
    QSqlQuery qry;
    qry.exec&#40;"insert into main.'"+table+"' ('"+column+"'&#41; values ('"+data+"')"); // insert in db
    qDebug() << "Written "<< data <<" to db";
    // read from db
    QSqlQueryModel *Model = new QSqlQueryModel;
    qry.exec&#40;"SELECT FROM "+column+" main.'"+table+"'"&#41;;
    int readData = Model->data(Model->index(0,0)).toInt();
    qDebug() << "Data read: " << readData;@

    Saving to the SQLite db is no problem, but I cant seem to load from it.
    I tried this, but that didn't work:
    @int readData = Model->data(Model->index(1,1)).toInt();@

    Please tell me if you know a way to get sqlitedata to an int variable.


Log in to reply