Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DATABASE.H
- #ifndef DATABASE_H
- #define DATABASE_H
- #include <QObject>
- #include <QSqlDatabase>
- #include <QDebug>
- #include <QSqlError>
- #include <QSqlQuery>
- #include "docs.h"
- class Database : public QObject
- {
- Q_OBJECT
- public:
- explicit Database(QObject *parent = 0);
- Q_INVOKABLE QVariant getAll();
- private:
- QSqlDatabase db;
- };
- #endif // DATABASE_H
- DATABASE.CPP
- #include "database.h"
- Database::Database(QObject *parent) : QObject(parent)
- {
- db = QSqlDatabase::addDatabase("QSQLITE");
- db.setDatabaseName("doctor.db");
- }
- QVariant Database::getAll()
- {
- QList<Docs*> docs;
- if (db.open()) {
- QSqlQuery qry("SELECT * FROM docs;");
- if (!qry.lastError().isValid()) {
- while (qry.next()) {
- docs.append(new Docs(qry.value(0).toInt(),
- qry.value(1).toString(),
- qry.value(2).toString()));
- }
- } else {
- qDebug() << qry.lastError().text();
- }
- } else {
- qDebug() << db.lastError().text();
- }
- return QVariant::fromValue(docs);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement