0

I have written a Qt program in c++ inorder to access a database and load database tables to table view! My program does not show any compile errors but gives 2 runtime errors

QSqlDatabasePrivate::removeDatabase: connection 'MyConnection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'MyConnection', old connection removed.

There are mainly to classes namely MainWindow(Has database connection methods) and Dialog Following are my codes

MainWindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

public: QSqlDatabase mydb;
        bool conOpen(QString userName,QString password,QString hostname,int port,QString service){
            mydb=QSqlDatabase::addDatabase("QOCI","MyConnection");
            mydb.setUserName(userName);
            mydb.setPassword(password);
            mydb.setHostName(hostname);
            mydb.setPort(port);
            mydb.setDatabaseName(service);
            return mydb.open();

}

MainWindow.cpp

void MainWindow::on_pushButton_clicked()
{
    Dialog *dialog1 = new Dialog(this);
   if(conOpen(ui->uname->text(),ui->pword->text(),ui->ip->text(),ui->port->text().toInt(),ui->service->text())){

        dialog1->show();
}

Dialog.h

class Dialog : public QDialog
{
    Q_OBJECT

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

private slots:
    void on_pushButton_clicked();

Dialog.cpp

void Dialog::on_pushButton_clicked()
{

    QSqlQueryModel *modal = new QSqlQueryModel();
    if(QSqlDatabase::contains("MyConnection")){

        QSqlQuery* qry=new QSqlQuery(QSqlDatabase::database("MyConnection"));
        qry->prepare("select NAME FROM Employees");
           qry->exec();
        modal->setQuery(*qry);
      ui->tableView->setModel(modal);

    }
}

The MainWindow form is used for login and Dialog form is used to retrieve entries in database table to a table view How can I correct the issue?

5
  • Do you press that button more than once? Commented Sep 20, 2017 at 4:46
  • @HiI'mFrogatto yeah! I pressed the button in MainWindow several times but the problem is that dialog1 does not appear though I press the button :( Commented Sep 20, 2017 at 4:49
  • That's the problem. QSqlDatabase::addDatabase isn't meant to be called more that once. Commented Sep 20, 2017 at 4:50
  • yeah but why doesn't dialog1 appear when I click the button? :( Commented Sep 20, 2017 at 4:51
  • Have you tried to debug your program? Is dialog1->show(); called the first time? Commented Sep 21, 2017 at 8:31

1 Answer 1

1

QSqlDatabase::addDatabase is a static function and every time you call it, it will create a connection instance. So if your app is designed to establish only a single connection to database server, you should not call it more that once.

QSqlDatabase QSqlDatabase::addDatabase(const QString &type, const QString &connectionName = QLatin1String( defaultConnection ))

Adds a database to the list of database connections using the driver type and the connection name connectionName. If there already exists a database connection called connectionName, that connection is removed.

You may need to move the logic of opening database code (including QSqlDatabase::addDatabase) to somewhere that gets called only once throughout the entire app.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanx! The problem is dialog1 not appearing when I click the button! But why doen't it appear?
@KushanPeiris Return value of conOpen may be false.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.