Help with running a select statement from a button click
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
Code:
#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
(mainwindow.cpp)
Code:
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;
}
}
dal.h
Code:
#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
Reply With Quote
dal.cpp
Code:
#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();
}
Re: Help with running a select statement from a button click
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 ;)
Re: Help with running a select statement from a button click
Quote:
Originally Posted by
Lykurg
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.
Re: Help with running a select statement from a button click
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:
Code:
void MainWindow:: on_pushButton_clicked()
{
// empty?
}
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;
} // end of the DatabaseManager::getPerson() definition
Where and how do you call getPerson()?
Re: Help with running a select statement from a button click
Quote:
Originally Posted by
ChrisW67
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:
Code:
void MainWindow:: on_pushButton_clicked()
{
// empty?
}
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;
} // 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
Re: Help with running a select statement from a button click
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.
Re: Help with running a select statement from a button click
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.
Re: Help with running a select statement from a button click
Code:
void MainWindow:: on_pushButton_clicked()
{
YourDatabaseManager->getPerson(/*...*/); // like you would call any other function
}
Re: Help with running a select statement from a button click
[CODE]
Quote:
Originally Posted by
Lykurg
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.
Re: Help with running a select statement from a button click
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.
Re: Help with running a select statement from a button click
Thanks for everyone you replied i have started reading alot more on C++.
Could anyone please explain to me the below.
Code:
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.
Re: Help with running a select statement from a button click
It's a reference to a pointer and I have never seen any practical use of it.