Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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)}


  • Lifetime Qt Champion

    Hi and welcome to devnet,

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


Log in to reply