Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Load specific sqlite column and row into an integer variable
Forum Updated to NodeBB v4.3 + New Features

Load specific sqlite column and row into an integer variable

Scheduled Pinned Locked Moved Mobile and Embedded
1 Posts 1 Posters 946 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    whitelatino
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved