Using single global object to access database



  • I am new to Qt programming! I have created a class named MainWIndow which has methods to provide database connection! I nee to create a global object of MainWindow class inorder to use it in several classes(MainWindow and Dialog)! following is my code!

    MainWindow.h

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:

     QSqlDatabase mydb;
    
    bool getconnOpen(QString uname,QString pword,QString ip,int port,QString dbname){
        mydb=QSqlDatabase::addDatabase("QOCI","MyDB");
        mydb.setUserName(uname);
        mydb.setPassword(pword);
        mydb.setHostName(ip);
        mydb.setPort(port);
        mydb.setDatabaseName(dbname);
    

    mydb.open
    return true;
    }

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::MainWindow *ui;

    };

    MainWindow.cpp

    void MainWindow::on_pushButton_clicked()
    {

    Dialog *dialog1=new Dialog(this);
    if(getconnOpen(ui->lineEdit->text(),ui->lineEdit_2->text(),ui->lineEdit_3->text(),ui->lineEdit_4->text().toInt(),ui->lineEdit_5->text()){
        dialog1->show();
    }
    

    }

    Dialog.h

    class Dialog : public QDialog
    {

    Q_OBJECT
    

    public:QSqlDatabase mydb;
    public:
    explicit Dialog(QWidget *parent = 0);

    ~Dialog();
    

    private slots:
    void on_pushButton_clicked();

    Dialog.cpp

    void Dialog::on_pushButton_clicked()
    {
    QSqlQueryModel modal = new QSqlQueryModel();
    QSqlQuery
    qry=new QSqlQuery(mydb);

    qry->prepare("select NAME FROM TEST1");
    qry->exec();
    modal->setQuery(*qry);
    ui->tableView->setModel(modal);

    }

    Since the connection opened is not accessible by Dialog.cpp as a result the query in dialog.cpp gives an error stating "database not open" ! I think this is because the connection is not a global 1! How can I place an object of MainWindow which can be accessed from several forms in a global position such that I can open a common database connection accessible by both MainWindow.cpp and dialog.cpp?



  • Hi @Kushan

    I think that you have to use :

    QSqlDatabase::addDatabase static functions

    The doc says:

    The database connection is referred to by connectionName. The newly added database connection is returned.
    .
    .
    .
    If connectionName is not specified, the new connection becomes the default connection for the application, and subsequent calls to database() without the connection name argument will return the default connection. If a connectionName is provided here, use database(connectionName) to retrieve the connection.

    You have the set the name of your database opened on your mainwindow.cpp

    I think that you can do something like this, on your Dialog:

    I think that you can something like this on your dialog.cpp:

    void Dialog::on_pushButton_clicked()
    {
    
    QSqlQueryModel modal = new QSqlQueryModel();
    mydb = QSqlDatabase::addDatabase("QOCI","MyDB");//you must add "MyDb" , otherwise Qt will look for the default database
    QSqlQuery qry=new QSqlQuery(mydb);
    qry->prepare("select NAME FROM TEST1");
    qry->exec();
    modal->setQuery(*qry);
    ui->tableView->setModel(modal);
    }
    

  • Lifetime Qt Champion

    Hi,

    There's no need for any global objects all the more if you only have one active connection for your application but it's also valid if you are managing multiple connections. The QSqlDatabase class already provides everything you need.

    Like written in the documentation, if you are only using one connection, don't give it a name, it will become the default connection and all the SQL related classes will use it by default.

    If you have several connections, it's easy to retrieve the one you are interested in by using QSqlDatabase::database.

    On a side note, you should properly test the outcome of your open call. Right now it could be failing and you don't care about that possibility.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.