PDA

View Full Version : Help with running a select statement from a button click



jins
15th November 2010, 21:41
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;
}

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




(mainwindow.cpp)


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;
}

}





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
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) :
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();
}

Lykurg
15th November 2010, 21:59
Hi,

several notes:


What does not work? What does the error say. See QSqlQuery::lastError().
We have [CODE] tags here to make source code better to read.
Do not store a private member to a QSqlDatabase. Use the global (singleton) QSqlDatabase::database() in a local scope where you need it (so that is gets destroyed right afterwards).
Even if it isn't a problem here, have a look at QSqlQuery::prepare().
Don't use "*" and then address the fields on there index. Databases could (even if it is not likely) return the fields in a different order than you expect. So better use something like "SELECT id, name, foo, bar FROM ..." then you can be sure about the order.


that's all for now ;)

jins
16th November 2010, 00:32
Hi,

several notes:


What does not work? What does the error say. See QSqlQuery::lastError().
We have [CODE] tags here to make source code better to read.
Do not store a private member to a QSqlDatabase. Use the global (singleton) QSqlDatabase::database() in a local scope where you need it (so that is gets destroyed right afterwards).
Even if it isn't a problem here, have a look at QSqlQuery::prepare().
Don't use "*" and then address the fields on there index. Databases could (even if it is not likely) return the fields in a different order than you expect. So better use something like "SELECT id, name, foo, bar FROM ..." then you can be sure about the order.


that's all for now ;)



Hi Lykurg, thank you very much for your reply, i will try and reply best i can.

* What does not work? What does the error say. See QSqlQuery::lastError().

There are no errors at moment as the application just starts up and when i click on the pushButton it does nothing which is correct as i wanted the pushButton to connect to a sqlite database and run a sql statment and display the results in the listwidget

As for your other recommendations i will try and slowly digest so i can try and implement into the code. If in the meantime if you could recommend you advise a way (Code examples) i could achieve my goal i would really appriciate it.

The main issue i am having here is how to get text string from a lineedit widget and use as part of an sql statement to search a database.

Thanks again for your help, much appriciated.

ChrisW67
16th November 2010, 06:53
It helps if you paste the actual code that is causing issues. What you posted won't compile (with the definition of getPerson inside the method on_pushButton_Clicked()). Assuming your code looks more like:


void MainWindow:: on_pushButton_clicked()
{
// empty?
}

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;
} // end of the DatabaseManager::getPerson() definition

Where and how do you call getPerson()?

jins
16th November 2010, 10:09
It helps if you paste the actual code that is causing issues. What you posted won't compile (with the definition of getPerson inside the method on_pushButton_Clicked()). Assuming your code looks more like:


void MainWindow:: on_pushButton_clicked()
{
// empty?
}

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;
} // end of the DatabaseManager::getPerson() definition

Where and how do you call getPerson()?



Sorry if i am not explaining the issue very well. You are right the code would not compile with the getPerson inside the puchButton_clicked.

Is there some way i can do this i.e. run that same code but from that click button?

Thanks in advance

Lykurg
16th November 2010, 10:49
Write the code into on_pushButton_clicked() or call the function from there, or make getPerson a slot and connect it with the buttons click signal.

jins
16th November 2010, 10:55
Hi Lykurg

You have hit the nail on the head, could you maybe provide an example of how i could call that function from the pushButton, this would most definatley put me on the right track as this is what i am trying to do.

Appriciate your patience with me, thanks again.

Lykurg
16th November 2010, 11:02
void MainWindow:: on_pushButton_clicked()
{
YourDatabaseManager->getPerson(/*...*/); // like you would call any other function
}

jins
16th November 2010, 16:27
[CODE]void MainWindow:: on_pushButton_clicked()
{
YourDatabaseManager->getPerson(/*...*/); // like you would call any other function
}





Hi again

I have tried adding DatabaseManager->getPerson(); to pushButton clicked and i get the below error

unexpected unqualified '->' token


I suspected that would cause a problem as the autocomplete code feature in Qt creator didnt auto fill whille i was typing it.

Am i missing something obvious.

wysota
16th November 2010, 17:01
I would really, really, reaaally, really suggest that you learn a tiny bit of C++ before you start developing a real program. Your "problem" has nothing to do with Qt, it's simply that you don't know how to use C++ and it's not something we can teach you here.

jins
18th November 2010, 20:57
Thanks for everyone you replied i have started reading alot more on C++.

Could anyone please explain to me the below.



bool getPerson(int ID, PersonData*& PERSON);


I know that * and & is used as pointers and are put after the text. But what is *&

Thanks in advance.

wysota
20th November 2010, 08:27
It's a reference to a pointer and I have never seen any practical use of it.