Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Load specific sqlite column and row into an integer variable

    Mobile and Embedded
    1
    1
    868
    Loading More Posts
    • 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
      whitelatino last edited by

      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 Reply Quote 0
      • First post
        Last post