]> Johnzone git - UserSheets.git/commitdiff
adding humans to the database works
authorJohn Janus <mail@johnzone.org>
Mon, 27 Feb 2017 22:49:43 +0000 (23:49 +0100)
committerJohn Janus <mail@johnzone.org>
Mon, 27 Feb 2017 22:49:43 +0000 (23:49 +0100)
- signal should be declared in addHuman.qml, but connecting that seems
hard

AddHuman.qml
database.cpp
database.h
main.cpp
main.qml

index 0eae420c53e75680f091e55dbd12176fbf102615..d0b8befc75114d6586312247b41bfeb5fa110457 100644 (file)
@@ -1,12 +1,14 @@
 import QtQuick 2.7
 import QtQuick.Controls 2.0
 import QtQuick.Layouts 1.0
+import QtQuick.Controls 1.4
 
 ApplicationWindow {
-  id: root
+  id: addhumanroot
   //width: 200
   //height: 200
-  Text{
+
+  Label {
     id: lblname
     anchors.left: parent.left
     anchors.verticalCenter: name.verticalCenter
@@ -17,7 +19,46 @@ ApplicationWindow {
     id: name
     anchors.left: lblname.right
     anchors.top: parent.top
+    placeholderText: qsTr("Kawubke")
+  }
+
+  Label {
+      id: lblfirstname
+      anchors.left: parent.left
+      anchors.verticalCenter: firstname.verticalCenter
+      text: qsTr("Firstname: ")
+  }
+
+  TextField {
+    id: firstname
+    anchors.left: lblname.right
+    anchors.top: name.bottom
     placeholderText: qsTr("Karl-Heinz")
   }
 
+  Label {
+      id: lblbirthday
+      anchors.left: parent.left
+      anchors.top: lblfirstname.bottom
+  }
+
+  Calendar {
+      id: birthday
+      anchors.left: lblbirthday.right
+      anchors.top: firstname.bottom
+      weekNumbersVisible: true
+  }
+
+  Button {
+      id:ok
+      onClicked: {
+          addHuman(name.text, firstname.text, birthday.selectedDate)
+          addhumanroot.close()
+      }
+      text: qsTr("OK")
+      anchors.bottom: parent.bottom
+      anchors.right: parent.right
+      visible: true
+  }
+
 }
index 1deeb704a097d022a1e6c9f7e5373d4abd6981ce..be1c6ac118b968701877eaa25c0c9f78c746295e 100644 (file)
@@ -1,6 +1,7 @@
 #include "database.h"
 #include <QtSql/QSqlQuery>
 #include <QDebug>
+#include <QDate>
 
 DataBase::DataBase(QObject *parent) : QObject(parent)
 {
@@ -18,31 +19,54 @@ DataBase::~DataBase()
 
 }
 
-QVariant DataBase::addHuman(const QString& name, const QString& firstname, const QDate& birthday)
+void DataBase::addHuman(const QVariant& name, const QVariant& firstname,
+                            const QVariant& birthday)
 {
+  qDebug()<<"addHuman slot called with: "<<name<<", "<<firstname<<", "<< birthday;
   QSqlQuery q;
+  if (!q.prepare(this->sqlAddHuman)) qDebug() << "error while preparing: " << q.lastError();
+  q.addBindValue(name.toString());
+  q.addBindValue(firstname.toString());
+  q.addBindValue(birthday.toDate());
+  if (!q.exec())qDebug()<<"error: "<< q.lastError();
 
 }
 
-QVariant DataBase::addService(const QString& name, const QUrl& url, const QString& description)
+void DataBase::addService(const QString& name, const QUrl& url,
+                              const QString& description)
 {
-
+  QSqlQuery q;
+  q.prepare(this->sqlAddService);
+  q.addBindValue(name);
+  q.addBindValue(url);
+  q.addBindValue(description);
+  q.exec();
 }
 
-QVariant DataBase::addUser(const QVariant& humanID, const QVariant& serviceID, const QString& username, const QString& password)
+void DataBase::addUser(const QVariant& humanID, const QVariant& serviceID,
+                           const QString& username, const QString& password)
 {
-
+  QSqlQuery q;
+  q.prepare(this->sqlAddUser);
+  q.addBindValue(humanID);
+  q.addBindValue(serviceID);
+  q.addBindValue(username);
+  q.addBindValue(password);
+  q.exec();
 }
 
 QSqlError DataBase::initDB()
 {
   QStringList tables = db.tables();
   QSqlQuery q;
-  if (!tables.contains("human", Qt::CaseSensitivity::CaseInsensitive)){   if (!q.exec(this->sqlCreateHumanTable)) return q.lastError();}
+  if (!tables.contains("human", Qt::CaseSensitivity::CaseInsensitive))
+  { if (!q.exec(this->sqlCreateHumanTable)) return q.lastError();}
   else qDebug() << "table human already created";
-  if (!tables.contains("service", Qt::CaseSensitivity::CaseInsensitive)){ if (!q.exec(this->sqlCreateServiceTable)) return q.lastError();}
+  if (!tables.contains("service", Qt::CaseSensitivity::CaseInsensitive))
+  { if (!q.exec(this->sqlCreateServiceTable)) return q.lastError();}
   else qDebug() << "table service already created";
-  if (!tables.contains("user", Qt::CaseSensitivity::CaseInsensitive)){    if (!q.exec(this->sqlCreateUserTable)) return q.lastError();}
+  if (!tables.contains("user", Qt::CaseSensitivity::CaseInsensitive))
+  { if (!q.exec(this->sqlCreateUserTable)) return q.lastError();}
   else qDebug() << "table user already created";
 
   return QSqlError();
index ab1a1560021982d5a56ddbfbbbf4658e874d4aaa..56c80815f549933668ed2b6fdae1e8f51feae6bd 100644 (file)
@@ -17,21 +17,70 @@ public:
 signals:
 
 public slots:
-  QVariant addHuman(const QString& name, const QString& firstname, const QDate& birthday);
-  QVariant addService(const QString& name, const QUrl& url, const QString& description);
-  QVariant addUser(const QVariant& humanID, const QVariant& serviceID, const QString& username, const QString& password);
+  void addHuman(const QVariant& name, const QVariant& firstname,
+                    const QVariant& birthday);
+
+  void addService(const QString& name, const QUrl& url,
+                      const QString& description);
+
+  void addUser(const QVariant& humanID, const QVariant& serviceID,
+                   const QString& username, const QString& password);
 
 
 private:
   QSqlError initDB();
 
   QSqlDatabase db;
-  QLatin1String sqlCreateHumanTable = QLatin1String("create table human(id integer primary key, name varchar, firstname varchar, birthday date)");
-  QLatin1String sqlCreateServiceTable = QLatin1String("create table service(id integer primary key, name varchar, url varchar, desription varchar)");
-  QLatin1String sqlCreateUserTable = QLatin1String(
-        "create table user(id integer primary key, humanid integer not null, serviceid integer not null, username varchar, password varchar,"
+  const QLatin1String sqlCreateHumanTable = QLatin1String(
+        "create table human("
+        "id integer primary key autoincrement,"
+        "name varchar,"
+        "firstname varchar,"
+        "birthday date"
+        ")");
+
+  const QLatin1String sqlCreateServiceTable = QLatin1String(
+        "create table service("
+        "id integer primary key autoincrement,"
+        "name varchar,"
+        "url varchar,"
+        "desription varchar"
+        ")");
+
+  const QLatin1String sqlCreateUserTable = QLatin1String(
+        "create table user("
+        "id integer primary key autoincrement,"
+        "humanid integer not null,"
+        "serviceid integer not null,"
+        "username varchar,"
+        "password varchar,"
         "foreign key(humanid) references human(id),"
-        "foreign key(serviceid) references service(id))");
+        "foreign key(serviceid) references service(id)"
+        ")");
+
+  const QLatin1String sqlAddHuman = QLatin1String(
+      "insert or ignore into "
+      "human "
+      "(name, firstname, birthday)"
+      " values "
+      "(?,?,?)"
+      );
+
+  const QLatin1String sqlAddService = QLatin1String(
+      "insert or ignore into "
+      "service "
+      "(name, url, description)"
+      " values "
+      "(?,?,?)"
+      );
+
+  const QLatin1String sqlAddUser = QLatin1String(
+      "insert or ignore into "
+      "user "
+      "(humanid, serviceid, username, password)"
+      " values "
+      "(?,?,?,?)"
+      );
 };
 
 #endif // DATABASE_H
index cb3e89d3f5e31d38f0e079d1b3577d1dad65fe4a..5b26229f5e157a91eaf2b919fb20b6d733554442 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -1,6 +1,8 @@
 #include <QGuiApplication>
 #include <QQmlApplicationEngine>
 #include "database.h"
+#include <QList>
+#include <QVariant>
 
 int main(int argc, char *argv[])
 {
@@ -11,6 +13,15 @@ int main(int argc, char *argv[])
   engine.load(QUrl(QLatin1String("qrc:/main.qml")));
   DataBase db;
 
+  QList<QObject*> roots = engine.rootObjects();
+  QObject* addHuman = roots.first();
+  for (QObject* qo : roots) {
+    if (qo->objectName() == "root")
+    addHuman = qo;
+  }
+  if (addHuman != nullptr)
+    QObject::connect(addHuman,SIGNAL(addHuman(QVariant, QVariant, QVariant)),
+                     &db, SLOT(addHuman(QVariant, QVariant, QVariant)));
 
   return app.exec();
 }
index 8968866c4d3dfe3040c10a668715d42c0a09a1ff..b245609ab5b60aee1f773ec1764736530392600c 100644 (file)
--- a/main.qml
+++ b/main.qml
@@ -8,6 +8,7 @@ ApplicationWindow {
   width: 640
   height: 480
   title: qsTr("Hello World")
+  signal addHuman(var name, var firstname, var birthday)
 
   Button {
     id: btnaddhuman