Selecting a database while using QSqlTableModel



  • I am going through the Qt documentation related to working with MySQL:
    http://doc.qt.io/qt-5/sql-model.html

    I have managed to run QQuery and retrieve some data from my test database, but when I tried to follow instructions in the Using the SQL Model Classes section, I didn't get any result. The example I have adapted to my database is:

        QSqlTableModel model;
        model.setTable("employee");
        model.setFilter("salary > 50000");
        model.setSort(2, Qt::DescendingOrder);
        model.select();
    
        for (int i = 0; i < model.rowCount(); ++i) {
            QString name = model.record(i).value("name").toString();
            int salary = model.record(i).value("salary").toInt();
            qDebug() << name << salary;
        }
    

    Adapted version:

        QSqlTableModel model;
        model.setTable("groups");
        model.select();
    
        for (int i = 0; i < model.rowCount(); ++i) {
            int groupNumber = model.record(i).value("number").toInt();
            cout << groupNumber << endl;
        }
    

    I am trying to select the groups table with only one column (number) (which holds primary keys). I have established connections with two databases, named live and arch, and would like to use one of them for this task.

    I have googled for any information related to this topic, but had no luck. Could someone please give advice and guidance? It would be much appreciated.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You can select the database when constructing your QSqlTableModel object. See the QSqlTableModel documentation.



  • @SGaist Hi, thank you for your reply and 'welcome'. I have tried to follow the QSqlTableModel documentation you have provided the link to. I am struggling to understand what should I provide as the first argument (parent). Could you please advise? This is the code I have used:

    //Name of the connection I want to use
    QString database = "arch";
    //Declaring a pointer to a QObject to use as the parent (have no idea what should it be)
    QObject *parent;
    //Creating a QSqlDatabase object to pass as the database to the QSqlTableModel constructor
    QSqlDatabase db = QSqlDatabase::database(database);
    //Creating a pointer to a new QSqlTableModel object
    QSqlTableModel *model = new QSqlTableModel(parent, db);
    //Trying to get information about the selected database (doesn't return anything in this case)
    qDebug() << model->database();
    //Selecting the "groups" table
    model->setTable("groups");
    model->select();
    //Trying to list all entries of the table (doesn't output anything in this case)
    for(int i = 0; i < model->rowCount(); ++i)
    {
    	int groupNumber = model->record(i).value("number").toInt();
    	cout << groupNumber << endl;
    }
    

  • Qt Champions 2016

    @spektro37 said in Selecting a database while using QSqlTableModel:

    QObject *parent;

    Hi
    That is not ok. it will be a dangling pointer.
    Im not 100% sure why it needs the parent for, but if the normal usage ( auto deletion), you can safely use nullptr and then do
    delete model; when no longer needed.
    Or if inside main window or widget , simply use "this" pointer.
    ( and no delete on your own)



  • @mrjj Hi. Thank you for your advice, everything is working now. I should have known better, than declaring an uninitialised pointer.

    It would still be good to know why a parent is required, though. I hope someone would be able to shed some light.


  • Qt Champions 2016

    @spektro37
    Super :)
    My best guess is like for all the other widgets,

    http://doc.qt.io/qt-5/objecttrees.html

    So its owned by say mainwindow and it will delete it when
    it self are deleted. (just like buttons etc)



  • @mrjj
    It does make sense, if you put it that way.)

    Very useful article! Thanks for sharing.)


  • Qt Champions 2016

    @spektro37
    Well its used with all QWidgets so would make sense.

    Its something to be aware of when creating GUI and also the little note that
    any QWidget that are not given a owner/parent, will become a window.
    That can be surprised when coming from other frameworks.

    Say you make a new label

    QLabel *lab= new QLabel();
    

    Since its given no parent, it will become a window.

    That was pretty surprising to me first time as i wanted to insert it into the main window.

    So its good to know about.


Log in to reply