First of all thanks ChrisW67 for the Quick response
Coming to the Question now i am trying it this way ..
My code is
Connection. h
#ifndef CONNECTION_H
#define CONNECTION_H
#include <QMessageBox>
#include <QtSql/QSQLiteDriver>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QDebug>
static bool createConnection()
{
db.setDatabaseName("./RMD0_2.db");
db.open();
qDebug()<< db <<db.isOpen();
if (!db.open()) {
QMessageBox::critical(0, qApp
->tr
("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
return false;
}
return true;
}
#endif // CONNECTION_H
#ifndef CONNECTION_H
#define CONNECTION_H
#include <QMessageBox>
#include <QtSql/QSQLiteDriver>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QDebug>
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./RMD0_2.db");
db.open();
qDebug()<< db <<db.isOpen();
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
return true;
}
#endif // CONNECTION_H
To copy to clipboard, switch view to plain text mode
And the main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <connection.h>
#include <QMessageBox>
int main(int argc, char *argv[])
{
if (!createConnection()){
return 1;
}
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <connection.h>
#include <QMessageBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
if (!createConnection()){
return 1;
}
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
The mainwindow.cpp
void MainWindow::menuAction()
{
QString qexe
= "SELECT STAFF_ID FROM EVENT_STAFF WHERE EVENT_STAFF_ID = 2";
q.prepare(qexe);
if(!q.exec())
qDebug()<< q.lastError();
else
mm.show();
}
void MainWindow::menuAction()
{
QSqlQuery q ;
QString qexe= "SELECT STAFF_ID FROM EVENT_STAFF WHERE EVENT_STAFF_ID = 2";
q.prepare(qexe);
if(!q.exec())
qDebug()<< q.lastError();
else
mm.show();
}
To copy to clipboard, switch view to plain text mode
my out put is :
QSqlDatabase(driver=""QSQLITE"", database=""./RMD0_2.db"", host="""", port=-1, user="""", open=true) true
and when i click on button menuAction in the mainwindow.cpp
QSqlError(-1, "Unable to fetch row", "No query")
what can be the problem.?
Bookmarks