PDA

View Full Version : error while executing qt application



khadija123
13th April 2012, 23:08
hi,
I have a qt interface with 2 text box N_departement and department and a button add
the program is to add these two text boxes in the department table in a postgresql database
but I get the following error I do not know why
when I click the Add button
QSqlQuery::prepare: database not open
"QODBCResult::exec: No statement handle available" Error: ""
this is what i wrote:
nomenclature.h
<code type="cpp">
#ifndef NOMENCLATURE_H
#define NOMENCLATURE_H

#include <QMainWindow>
#include <QtSql>
namespace Ui {
class nomenclature;
}

class nomenclature : public QMainWindow
{
Q_OBJECT

public:
explicit nomenclature(QWidget *parent = 0);
~nomenclature();
void connexion_based();

private:
Ui::nomenclature *ui;
QSqlDatabase db;

private slots:
void on_Ajouter_clicked();

};

#endif // NOMENCLATURE_H
</code>
nomenclature.cpp
<code type="cpp">
#include "nomenclature.h"
#include "ui_nomenclature.h"
#include <QtSql>
nomenclature::nomenclature(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::nomenclature)
{ this->connexion_based();
ui->setupUi(this);
QObject::connect(ui->Ajouter,SIGNAL(clicked()),this,SLOT(on_Ajouter_cli cked()));
}

nomenclature::~nomenclature()
{
delete ui;
}
void nomenclature::connexion_based()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setUserName("postgres");
db.setPassword("khadija");
db.setDatabaseName("d");
db.open();
if(db.open())
{
qDebug()<< "opened" ;
db.close();
}
else
{
qDebug() << "ERROR" << db.lastError().text();
}

}


void nomenclature::on_Ajouter_clicked()
{
QSqlQuery sql(db);
sql.prepare("INSERT INTO departement VALUES (:n_dep, :libellé_dep);");
sql.bindValue(":n_dep",ui->n_departement->text().toInt());
sql.bindValue(":libellé_dep",ui->intitule_departement->text());
sql.exec();
}
</code>
main.cpp
<code type="cpp">
#include <QtGui/QApplication>
#include "nomenclature.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
nomenclature w;
w.show();

return a.exec();
}
</code>
can you help me please
ajouter une réponsethank you

ChrisW67
13th April 2012, 23:16
You most likely do not have the ODBC (the one you are trying to use) or PostgreSQL (QPSQL, the one you probably should use) Qt plugins built and installed.

BTW: Your code tags are wrong.