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.setConnectOptions();
    
    ptr_mydb.setDatabaseName("/home/stud/sqlite/ghdb.sqlite"); // database path on devkit
    ptr_mydb.open();
    if(ptr_mydb.open())
    {
        qDebug() << "Database connection established";
    }
    else
    {
        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;;
    Model->setQuery(qry);
    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.

    Thanks


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.