From 9c720242f7d29e93c918c07d48dab07ef3e0e0fe Mon Sep 17 00:00:00 2001 From: John Janus Date: Sun, 6 Aug 2017 09:35:09 +0200 Subject: [PATCH] fix error in create customer table --- dbconn.cpp | 40 ++++++++++++++++++++++------------- dbconn.h | 46 ++++++++++++++++++++++++----------------- dialogs/AddCustomer.qml | 5 +++-- 3 files changed, 56 insertions(+), 35 deletions(-) diff --git a/dbconn.cpp b/dbconn.cpp index 9720e29..94feb15 100644 --- a/dbconn.cpp +++ b/dbconn.cpp @@ -18,19 +18,24 @@ QSqlError DbConn::initDB() const { QStringList tables = db.tables(); QSqlQuery q; - if (!tables.contains (this->tabNameCustomers, Qt::CaseSensitivity::CaseInsensitive) ) { + if (!tables.contains (this->tabNameCustomers, + Qt::CaseSensitivity::CaseInsensitive) ) { if (!q.exec (this->sqlCreateCustomerTable) ) return q.lastError(); } - if (!tables.contains (this->tabNameServices, Qt::CaseSensitivity::CaseInsensitive) ) { + if (!tables.contains (this->tabNameServices, + Qt::CaseSensitivity::CaseInsensitive) ) { if (!q.exec (this->sqlCreateServicesTable) ) return q.lastError(); } - if (!tables.contains (this->tabNameOrders, Qt::CaseSensitivity::CaseInsensitive) ) { + if (!tables.contains (this->tabNameOrders, + Qt::CaseSensitivity::CaseInsensitive) ) { if (!q.exec (this->sqlCreateOrdersTable) ) return q.lastError(); } - if (!tables.contains (this->tabNameOrderitems, Qt::CaseSensitivity::CaseInsensitive) ) { + if (!tables.contains (this->tabNameOrderitems, + Qt::CaseSensitivity::CaseInsensitive) ) { if (!q.exec (this->sqlCreateOrderItemsTable) ) return q.lastError(); } - if (!tables.contains (this->tabNameInvoices, Qt::CaseSensitivity::CaseInsensitive) ) { + if (!tables.contains (this->tabNameInvoices, + Qt::CaseSensitivity::CaseInsensitive) ) { if (!q.exec (this->sqlCreateInvoiceTable) ) return q.lastError(); } @@ -40,7 +45,8 @@ QSqlError DbConn::initDB() const void DbConn::addService (const QString& name, const int& cost) const { QSqlQuery q; - if (!q.prepare (this->sqlAddService) ) qCCritical (dbase) << "error while preparing: " << q.lastError(); + if (!q.prepare (this->sqlAddService) ) + qCCritical (dbase) << "error while preparing: " << q.lastError(); q.addBindValue (name ); q.addBindValue (cost ); if (!q.exec() ) qCCritical (dbase) << "error: " << q.lastError(); @@ -49,14 +55,16 @@ void DbConn::addService (const QString& name, const int& cost) const qCDebug (dbase) << "added new service " << name << ", costing " << cost; } } -void DbConn::addCustomer (const QString& name, const QString& street, const QString& number, - const QString& city, const uint8_t& zip) const +void DbConn::addCustomer (const QString& name, + const QString& street, const QString& number, + const QString& city, const int& zip) const { QSqlQuery q; - if (!q.prepare (this->sqlAddCustomer) ) qCCritical (dbase) << "error while preparing: " << q.lastError(); + if (!q.prepare (this->sqlAddCustomer) ) + qCCritical (dbase) << "error while preparing: " << q.lastError(); q.addBindValue (name ); q.addBindValue (street); - q.addBindValue (number ); + q.addBindValue (number); q.addBindValue (city); q.addBindValue (zip ); if (!q.exec() ) qCCritical (dbase) << "error: " << q.lastError(); @@ -66,10 +74,12 @@ void DbConn::addCustomer (const QString& name, const QString& street, const QStr << street << " " << number << ", " << zip << " " << city; } } -void DbConn::addOrder (const int& customerID, const QDate& date, const QList& items) const +void DbConn::addOrder (const int& customerID, const QDate& date, + const QList& items) const { QSqlQuery q; - if (!q.prepare (this->sqlAddOrder) ) qCCritical (dbase) << "error while preparing: " << q.lastError(); + if (!q.prepare (this->sqlAddOrder) ) + qCCritical (dbase) << "error while preparing: " << q.lastError(); q.addBindValue (customerID ); q.addBindValue (date ); @@ -77,7 +87,8 @@ void DbConn::addOrder (const int& customerID, const QDate& date, const QListsqlAddOrderItem) ) qCCritical (dbase) << "error while preparing: " << q.lastError(); + if (!q.prepare (this->sqlAddOrderItem) ) + qCCritical (dbase) << "error while preparing: " << q.lastError(); QList orderIDList; for (int i = 0; i < items.length(); i++) { @@ -93,7 +104,8 @@ void DbConn::addOrder (const int& customerID, const QDate& date, const QListsqlAddInvoice) ) qCCritical (dbase) << "error while preparing: " << q.lastError(); + if (!q.prepare (this->sqlAddInvoice) ) + qCCritical (dbase) << "error while preparing: " << q.lastError(); q.addBindValue (date ); q.addBindValue (orderID ); if (!q.exec() ) qCCritical (dbase) << "error: " << q.lastError(); diff --git a/dbconn.h b/dbconn.h index 8c8065c..74d881e 100644 --- a/dbconn.h +++ b/dbconn.h @@ -12,7 +12,31 @@ class DbConn : public QObject Q_OBJECT QSqlDatabase db; QSqlError initDB() const; - const QLatin1String tabNameCustomers {"customers"}; + +public: + explicit DbConn (QObject* parent = nullptr); + +signals: + void servicesChanged() const; + void customersChanged() const; + void ordersChanged() const; + void orderitemsChanged() const; + void invoicesChanged() const; + +public slots: + void addService (const QString& name, const int& cost) const; + void addCustomer (const QString& name, + const QString& street, const QString& number, + const QString& city, const int& zip) const; + void addOrder (const int& customerID, const QDate& date, + const QList& items) const; + void addInvoice (const QDate& date, const int& OrderID) const; + +private: + + const QLatin1String tabNameCustomers + {"customers" + }; const QLatin1String tabNameOrders {"orders"}; const QLatin1String tabNameOrderitems {"orderitems"}; const QLatin1String tabNameInvoices{"invoices"}; @@ -22,7 +46,7 @@ class DbConn : public QObject "id integer primary key autoincrement," "name text not null," "street text," - "number text" + "number text," "city text," "zip integer" ")" @@ -32,7 +56,7 @@ class DbConn : public QObject " customers " "(name, street, number, city, zip)" " values " - "(?,?,?,?,?)" + "(?, ?, ?, ?, ?)" }; const QLatin1String sqlCreateServicesTable { "create table services(" @@ -94,22 +118,6 @@ class DbConn : public QObject " values " "(?,?)" }; -public: - explicit DbConn (QObject* parent = nullptr); - -signals: - void servicesChanged() const; - void customersChanged() const; - void ordersChanged() const; - void orderitemsChanged() const; - void invoicesChanged() const; - -public slots: - void addService (const QString& name, const int& cost) const; - void addCustomer (const QString& name, const QString& street, const QString& number, - const QString& city, const uint8_t& zip) const; - void addOrder (const int& customerID, const QDate& date, const QList& items) const; - void addInvoice (const QDate& date, const int& OrderID) const; }; #endif // DBCONN_H diff --git a/dialogs/AddCustomer.qml b/dialogs/AddCustomer.qml index 57a5356..c75b790 100644 --- a/dialogs/AddCustomer.qml +++ b/dialogs/AddCustomer.qml @@ -9,11 +9,12 @@ Dialog { modality: Qt.WindowModal title: qsTr("Add Customer") standardButtons: StandardButton.Save | StandardButton.Cancel - onAccepted: dbconn.addCustomer(nameIn.text, streetIn.text, numberIn.text, cityIn.text, zipIn.text) + onAccepted: dbconn.addCustomer(nameIn.text, streetIn.text, numberIn.text, + cityIn.text, zipIn.text) GridLayout { columns: 2 Label {text: qsTr("Name")} - TextField {id: textIn} + TextField {id: nameIn} Label {text: qsTr("Street")} TextField {id: streetIn} -- 2.47.0