From: John Janus Date: Mon, 27 Feb 2017 22:49:43 +0000 (+0100) Subject: adding humans to the database works X-Git-Url: https://git.johnzone.org/?a=commitdiff_plain;h=32473bb914fb330cfdc36175e87e7c2c77b9a711;p=UserSheets.git adding humans to the database works - signal should be declared in addHuman.qml, but connecting that seems hard --- diff --git a/AddHuman.qml b/AddHuman.qml index 0eae420..d0b8bef 100644 --- a/AddHuman.qml +++ b/AddHuman.qml @@ -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 + } + } diff --git a/database.cpp b/database.cpp index 1deeb70..be1c6ac 100644 --- a/database.cpp +++ b/database.cpp @@ -1,6 +1,7 @@ #include "database.h" #include #include +#include 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: "<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(); diff --git a/database.h b/database.h index ab1a156..56c8081 100644 --- a/database.h +++ b/database.h @@ -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 diff --git a/main.cpp b/main.cpp index cb3e89d..5b26229 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,8 @@ #include #include #include "database.h" +#include +#include 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 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(); } diff --git a/main.qml b/main.qml index 8968866..b245609 100644 --- 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