Hi everyone
This is my first post so wish me luck!
I am new to C++ and QT, i have tried to write a small application that will search an sqlite database. But i cannot figure out how to run a select statement againts the database from a pushButton_clicked
Any help would really get me started, thanks in advance.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
private slots:
void on_pushButton_2_clicked();
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
private slots:
void on_pushButton_2_clicked();
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
(mainwindow.cpp)
void MainWindow:: on_pushButton_clicked()
{
bool DatabaseManager::getPerson(int ID, PersonData*& PERSON)
{
bool ret = false;
if (query.next())
{
PERSON->ID = query.value(0).toInt();
PERSON->FIRSTNAME = query.value(1).toString();
PERSON->LASTNAME = query.value(2).toString();
PERSON->AGE = query.value(3).toInt();
ret = true;
}
return ret;
}
}
void MainWindow:: on_pushButton_clicked()
{
bool DatabaseManager::getPerson(int ID, PersonData*& PERSON)
{
bool ret = false;
QSqlQuery query(QString("select * from PERSON where ID = %1").arg(ID));
if (query.next())
{
PERSON->ID = query.value(0).toInt();
PERSON->FIRSTNAME = query.value(1).toString();
PERSON->LASTNAME = query.value(2).toString();
PERSON->AGE = query.value(3).toInt();
ret = true;
}
return ret;
}
}
To copy to clipboard, switch view to plain text mode
dal.h
#ifndef DAL_H
#define DAL_H
#include <QObject>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QString>
class PersonData
{
public:
int ID;
int AGE;
};
class DatabaseManager
: public QObject{
public:
DatabaseManager
(QObject *parent
= 0);
~DatabaseManager();
public:
bool openDB();
bool getPerson(int ID, PersonData*& PERSON);
private:
};
#endif // DAL_H
#ifndef DAL_H
#define DAL_H
#include <QObject>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QString>
class PersonData
{
public:
int ID;
QString FIRSTNAME;
QString LASTNAME;
int AGE;
};
class DatabaseManager : public QObject
{
public:
DatabaseManager(QObject *parent = 0);
~DatabaseManager();
public:
bool openDB();
bool getPerson(int ID, PersonData*& PERSON);
private:
QSqlDatabase db;
};
#endif // DAL_H
To copy to clipboard, switch view to plain text mode
Reply With Quote
dal.cpp
#include "dal.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QString>
#include <mainwindow.h>
#include <ui_mainwindow.h>
MainWindow
::MainWindow(QWidget *parent
) :ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool DatabaseManager:penDB()
{
// Find QSLite driver
#ifdef Q_OS_LINUX
// NOTE: We have to store database file into user home folder in Linux
path.
append(QDir::separator()).
append("mydb.sqlite");
path
= QDir::toNativeSeparators(path
);
db.setDatabaseName(path);
#else
// NOTE: File exists in the application private folder, in Symbian Qt implementation
db.setDatabaseName("mydb.sqlite");
#endif
// Open databasee
return db.open();
}
#include "dal.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QString>
#include <mainwindow.h>
#include <ui_mainwindow.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool DatabaseManager:penDB()
{
// Find QSLite driver
db = QSqlDatabase::addDatabase("QSQLITE");
#ifdef Q_OS_LINUX
// NOTE: We have to store database file into user home folder in Linux
QString path(QDir::home().path());
path.append(QDir::separator()).append("mydb.sqlite");
path = QDir::toNativeSeparators(path);
db.setDatabaseName(path);
#else
// NOTE: File exists in the application private folder, in Symbian Qt implementation
db.setDatabaseName("mydb.sqlite");
#endif
// Open databasee
return db.open();
}
To copy to clipboard, switch view to plain text mode
Bookmarks