View Full Version : QSqlDatabase usage
goes2bob
7th February 2008, 00:11
I have an application that is using an SQLite database. I am trying to follow Qt examples by connecting via the conneciton.h file, but do not seem to get it.
I have main.cpp call gui.cpp which creates a stackedWidget with 4 widgets, 2 of these will (may) someday include QTableViews that are simple read-only tables from a database. I have seperated the 4 different widgets into their own classes (which are also in different files - not sure if that is important or not). I am trying to have one of the widget classes query the db and the query returns false.
Where should I actually put the "createConnection()" call (it's in main.cpp right now), and how do I get a pointer (or something else) to use that my query can use. I think I am thinking too much - cannot see thru the fog :o.
-Thanks in advance
Goes2bob
ashukla
7th February 2008, 05:42
Where should I actually put the "createConnection()" call (it's in main.cpp right now), and how do I get a pointer (or something else) to use that my query can use. I think I am thinking too much - cannot see thru the fog
main.cpp is a right place to call createConnection. You can use like this;
#include <QApplication>
#include "connection.h"
int main(int argc, char ** argv)
{
QApplication app( argc, argv );
if(!createConnection())
return app.exec();
......
......
......
return app.exec();
}
gboelter
10th February 2008, 03:21
I have main.cpp call gui.cpp which creates a stackedWidget with 4 widgets, 2 of these will (may) someday include QTableViews that are simple read-only tables from a database.
I'm a beginner too, so may be I am not right, but my main.cpp looks only like this:
// main.cpp
#include <QApplication>
int main( int argc, char* argv[])
{
QApplication app(argc, argv);
Erpel myErpel;
myErpel.show();
return app.exec();
}
Then I have a file called Erpel.cpp. This is the file where I put the "createConnection()" call.
// Erpel.cpp
#include "Erpel.h"
Erpel::Erpel(QWidget *parent) : QMainWindow( parent )
{
setupUi(this);
stackedWidget -> setCurrentIndex(0);
connect( actionHome,SIGNAL (triggered()), this, SLOT(slotHome()) );
connect( actionBackup,SIGNAL (triggered()), this, SLOT(fileBackup()) );
connect( actionRestore,SIGNAL (triggered()), this, SLOT(fileRestore()) );
// more code follows here ....
}
bool Erpel::dbConnect(QString dbName)
{
QString curDir = QCoreApplication::applicationDirPath();
QString iniFile = curDir+"/erpel.ini"; // get some parameters from erpel.ini
QSettings settings(iniFile, QSettings::IniFormat);
QString dbdriver = settings.value("database/dbdriver").toString();
QString dbhost = settings.value("database/dbhost").toString();
QString dbname = settings.value("database/dbname").toString();
QString dbuser = settings.value("database/dbuser").toString();
QString dbpasswd = settings.value("database/dbpasswd").toString();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(dbhost);
db.setDatabaseName(dbName);
db.setUserName(dbuser);
db.setPassword(dbpasswd);
bool connect2Db = db.open(); // try to open the database ...
if (!connect2Db)
{
QMessageBox::critical(0, qApp->tr("ERROR!"),
qApp->trUtf8("I was not able to connect to your database!"),
QMessageBox::Cancel);
return false;
}
qDebug() << "dbconnect" << db.isValid();
return true;
}
and
// Erpel.h // not complete, only as a sample ....
#ifndef ERPEL_H
#define ERPEL_H
#include <QtGui>
#include <QtCore/QVariant>
#include <QSettings>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
#include <QDebug>
#include "ui_erpel.h"
class Erpel : public QMainWindow, public Ui::Erpel
{
Q_OBJECT
public:
Erpel (QWidget * parent = 0);
~Erpel();
QString dbdriver;
QString dbhost;
QString dbname;
QString dbuser;
QString dbpasswd;
bool dbConnect(QString dbName);
public slots:
void slotClose();
private:
void adjustHeader();
private slots:
void editButton(int name);
protected:
signals:
};
#endif //ERPEL_H
I think I am thinking too much - cannot see thru the fog :o.
I know that feeling very well, but may be the code above will help you a little bit to find your way even when it's foggy .... ;)
jpn
10th February 2008, 07:48
Perhaps you forgot to call Erpel::dbConnect()?
gboelter
10th February 2008, 15:03
Perhaps you forgot to call Erpel::dbConnect()?
Hello jpn,
I prefer to call Erpel::dbConnect() when I need some datas from my database, not during starting the program.
Like this for example:
void Erpel::showKasseLayout()
{
if (dbConnect(currentMandant))
{
QSqlQuery query;
QString image;
query.prepare("SELECT ID, Beschriftung, ArtikelNummer, Image, ToolTip, TimeStamp FROM kassenlayout");
query.exec();
if (query.isSelect())
query.first();
// more code follows here ....
I think it's a little bit more code and a little bit more to do for the computer, but it's safer.
Is that correct?
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.