QSqlQuery::prepare: database not open



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

    }



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



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



  • r u able to connect to db outside qt program ?





  • @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!



  • @mostefa Thanx mate


Log in to reply
 

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