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. QSqlQuery::prepare: database not open
QtWS25 Last Chance

QSqlQuery::prepare: database not open

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatorqocidatabaseopen
7 Posts 3 Posters 2.7k Views
  • 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.
  • L Offline
    L Offline
    Lasith
    wrote on 15 Sept 2017, 04:48 last edited by Lasith
    #1

    I am writing a program to access an oracle database( driver loaded successfully)! But when I run the program and click the button I get the above stated error!
    Following is my header file

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtSql>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public : QSqlDatabase mydb;

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    public slots:
    void getInput1();
    void getInput2();
    void getInput3();
    void getInput4();
    void getInput5();
    private slots:
    void on_pushButton_clicked();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H

    Following is my cpp file

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QtSql"
    #include "QString"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    mydb=QSqlDatabase::addDatabase("QOCI");
    connect(ui->uname,SIGNAL(returnPressed()),this,SLOT(getInput1()));
    connect(ui->pword,SIGNAL(returnPressed()),this,SLOT(getInput2()));
    connect(ui->ip,SIGNAL(returnPressed()),this,SLOT(getInput3()));
    connect(ui->port,SIGNAL(returnPressed()),this,SLOT(getInput4()));
    connect(ui->sname,SIGNAL(returnPressed()),this,SLOT(getInput5()));
    mydb.open();
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::getInput1(){
    QString a=ui->uname->text();
    mydb.setUserName(a);
    }
    void MainWindow::getInput2(){
    QString b=ui->pword->text();
    mydb.setPassword(b);
    }
    void MainWindow::getInput3(){
    QString c=ui->ip->text();
    mydb.setHostName(c);
    }
    void MainWindow::getInput4(){
    QString d=ui->port->text();
    int e = d.toInt();
    mydb.setPort(e);
    }
    void MainWindow::getInput5(){
    QString e=ui->sname->text();
    mydb.setDatabaseName(e);
    }

    void MainWindow::on_pushButton_clicked()
    {
    mydb.open();

    QSqlQueryModel *modal = new QSqlQueryModel();
    
    QSqlQuery* qry=new QSqlQuery(mydb);
    
    
    qry->prepare("select NAME FROM TEST1");
       qry->exec();
       modal->setQuery(*qry);
       ui->tableView->setModel(modal);
    

    }

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 15 Sept 2017, 05:14 last edited by
      #2

      since it is oracledb, it may be asking for u to give username, password,hostname etc inputs. please check it.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      L 1 Reply Last reply 15 Sept 2017, 05:17
      0
      • D dheerendra
        15 Sept 2017, 05:14

        since it is oracledb, it may be asking for u to give username, password,hostname etc inputs. please check it.

        L Offline
        L Offline
        Lasith
        wrote on 15 Sept 2017, 05:17 last edited by
        #3

        @dheerendra I've already given them using signals and slots

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 15 Sept 2017, 05:20 last edited by
          #4

          r u able to connect to db outside qt program ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          L 1 Reply Last reply 15 Sept 2017, 05:23
          0
          • D dheerendra
            15 Sept 2017, 05:20

            r u able to connect to db outside qt program ?

            L Offline
            L Offline
            Lasith
            wrote on 15 Sept 2017, 05:23 last edited by
            #5

            @dheerendra Yes!

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mostefa
              wrote on 15 Sept 2017, 06:39 last edited by mostefa
              #6

              @Lasith said in QSqlQuery::prepare: database not open:

              mydb.open();

              Hi i think that the reason of your problem is that you are opening database juste after SIGNAL/SLOT connections even if signals are not triggered,

              Just before

              mydb.open();
              

              you can try to add

              qDebug() << mydb.username() << mydb.hostname();
              

              Value of username will be empty and value of hostname will be empty too,

              The Qt doc says:

              bool QSqlDatabase::open ()
              

              Opens the database connection using the current connection values. Returns true on success; otherwise returns false.

              Or even you can do this after open():

              if(mydb.open())
              {
                 qDebug() << "database correctly opened";
              }
              else
              {
               qDebug() << mydb.username << mydb.hostname ; //these values will be null
              }
              

              I hope this can help you!

              L 1 Reply Last reply 15 Sept 2017, 06:49
              1
              • M mostefa
                15 Sept 2017, 06:39

                @Lasith said in QSqlQuery::prepare: database not open:

                mydb.open();

                Hi i think that the reason of your problem is that you are opening database juste after SIGNAL/SLOT connections even if signals are not triggered,

                Just before

                mydb.open();
                

                you can try to add

                qDebug() << mydb.username() << mydb.hostname();
                

                Value of username will be empty and value of hostname will be empty too,

                The Qt doc says:

                bool QSqlDatabase::open ()
                

                Opens the database connection using the current connection values. Returns true on success; otherwise returns false.

                Or even you can do this after open():

                if(mydb.open())
                {
                   qDebug() << "database correctly opened";
                }
                else
                {
                 qDebug() << mydb.username << mydb.hostname ; //these values will be null
                }
                

                I hope this can help you!

                L Offline
                L Offline
                Lasith
                wrote on 15 Sept 2017, 06:49 last edited by
                #7

                @mostefa Thanx mate

                1 Reply Last reply
                0

                2/7

                15 Sept 2017, 05:14

                topic:navigator.unread, 5
                • Login

                • Login or register to search.
                2 out of 7
                • First post
                  2/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved