Advertisement
Guest User

Untitled

a guest
May 10th, 2015
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. DATABASE.H
  2.  
  3. #ifndef DATABASE_H
  4. #define DATABASE_H
  5.  
  6. #include <QObject>
  7. #include <QSqlDatabase>
  8. #include <QDebug>
  9. #include <QSqlError>
  10. #include <QSqlQuery>
  11.  
  12. #include "docs.h"
  13.  
  14. class Database : public QObject
  15. {
  16.     Q_OBJECT
  17. public:
  18.     explicit Database(QObject *parent = 0);
  19.     Q_INVOKABLE QVariant getAll();
  20.  
  21. private:
  22.     QSqlDatabase db;
  23. };
  24.  
  25. #endif // DATABASE_H
  26.  
  27. DATABASE.CPP
  28.  
  29. #include "database.h"
  30.  
  31. Database::Database(QObject *parent) : QObject(parent)
  32. {
  33.     db = QSqlDatabase::addDatabase("QSQLITE");
  34.     db.setDatabaseName("doctor.db");
  35. }
  36.  
  37. QVariant Database::getAll()
  38. {
  39.     QList<Docs*> docs;
  40.     if (db.open()) {
  41.         QSqlQuery qry("SELECT * FROM docs;");
  42.         if (!qry.lastError().isValid()) {
  43.             while (qry.next()) {
  44.                 docs.append(new Docs(qry.value(0).toInt(),
  45.                                      qry.value(1).toString(),
  46.                                      qry.value(2).toString()));
  47.             }
  48.         } else {
  49.             qDebug() << qry.lastError().text();
  50.         }
  51.  
  52.     } else {
  53.         qDebug() << db.lastError().text();
  54.     }
  55.  
  56.     return QVariant::fromValue(docs);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement