Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Fetch data from a sqlite database to a label
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Fetch data from a sqlite database to a label

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 2.3k 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.
  • S Offline
    S Offline
    Syland3r
    wrote on last edited by Syland3r
    #1

    Hello, I'm new to Qt and and I'm trying to read data from a db to a label (actually I want that data on a combo box but if I get it to work on a label it shouldn't be hard to use that on the combo I think) so here's my code:

    //----------DB---------------------------------------------------

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("r3d.db");
    bool db_ok = db.open();
    
    //---------------------label-----------------------------
    
    
    QSqlQuery query("SELECT SITE FROM MASTER");
    QString example = query.value(1).toString();
    ui->label->setText(example);
    
    
    
    
    //---------------------------------------------------------------
    

    Also can you recommend a book or an online course, the documentation is good but for things like this I struggle a little.

    Thank You.

    [SOLUTION]

    So i did this:

    QSqlQuery query("SELECT SITE FROM MASTER");
    while (query.next()){
    ui->cB1->addItem(query.value(0).toString());

    Because what I really want was to put the data on a combo box I use the combo and for a label the code will be:

    QSqlQuery query("SELECT SITE FROM MASTER");
    while (query.next()){
    ui->label->setText(query.value(0).toString());

    Have in mind that using it this way only "stores" the current or last value read so that's why only works when use 0{query.value(0)}

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You never execute the request so you don't have any valid result.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      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