Hi all, could really do with some help and advice.
I have written a small app that will search a database by a users name
for example: select ID, FNAME, LNAME, AGE from PERSON where FNAME = $1
the app runs the above query and returns it into a QList which is then displayed in a QlistWidget
The Qlistwidget displays only a list of "FNAME" (users firstnames, which is exactly what i want at the moment)
Now what i need help to achieve:
I would like to click a users name in the listwidget list and display the entire users record details i.e. ID, FNAME, LNAME and AGE information.
I understand how to search for data, but i dont know how i would be able to get the users ID so that i may perform another select query against it to get the users entire record details.
Any help no matter how small would be really appriciated.
Thanks in advance.
dal.h
#ifndef DAL_H
#define DAL_H
#include <QObject>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QString>
#include <QVariant>
class PersonData
{
public:
PersonData();
~PersonData();
int ID;
int AGE;
};
{
public:
~Dal();
public:
bool openDB();
QList<PersonData*> getPerson();
private:
};
#endif // DAL_H
#ifndef DAL_H
#define DAL_H
#include <QObject>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QString>
#include <QVariant>
class PersonData
{
public:
PersonData();
~PersonData();
int ID;
QString FIRSTNAME;
QString LASTNAME;
int AGE;
};
class Dal : public QObject
{
public:
Dal(QObject *parent = 0);
~Dal();
public:
bool openDB();
QList<PersonData*> getPerson();
private:
QSqlDatabase db;
};
#endif // DAL_H
To copy to clipboard, switch view to plain text mode
dal.cpp
#include "dal.h"
{
}
Dal::~Dal()
{
if (db.isOpen())
db.close();
}
PersonData::PersonData()
{
ID = 0;
}
PersonData::~PersonData()
{
}
bool Dal::openDB()
{
// 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();
}
QList<PersonData*> Dal::getPerson()
{
QList<PersonData*> result;
QSqlQuery query
("select ID, FIRSTNAME, LASTNAME, AGE from PERSON");
while (query.next()) {
PersonData* item = new PersonData();
item->ID = query.value(0).toInt();
item->FIRSTNAME = query.value(1).toString();
item->LASTNAME = query.value(2).toString();
item->AGE = query.value(3).toInt();
result.append(item);
}
return result;
}
#include "dal.h"
Dal::Dal(QObject *parent):
QObject (parent)
{
}
Dal::~Dal()
{
if (db.isOpen())
db.close();
}
PersonData::PersonData()
{
ID = 0;
}
PersonData::~PersonData()
{
}
bool Dal::openDB()
{
// 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();
}
QList<PersonData*> Dal::getPerson()
{
QList<PersonData*> result;
QSqlQuery query("select ID, FIRSTNAME, LASTNAME, AGE from PERSON");
while (query.next()) {
PersonData* item = new PersonData();
item->ID = query.value(0).toInt();
item->FIRSTNAME = query.value(1).toString();
item->LASTNAME = query.value(2).toString();
item->AGE = query.value(3).toInt();
result.append(item);
}
return result;
}
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
void MainWindow::on_pushButton1_clicked()
{
ui->listWidget->clear();
persondata.clear();
persondata = dbManager->getPerson();
PersonData* persondata2;
foreach(persondata2,persondata)
{
ui->listWidget->addItem(persondata2->FIRSTNAME);
}
}
void MainWindow::on_pushButton1_clicked()
{
ui->listWidget->clear();
persondata.clear();
persondata = dbManager->getPerson();
PersonData* persondata2;
foreach(persondata2,persondata)
{
ui->listWidget->addItem(persondata2->FIRSTNAME);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks