diff --git a/dbmanager.cpp b/dbmanager.cpp new file mode 100644 index 0000000..af56e79 --- /dev/null +++ b/dbmanager.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include + +DbManager::DbManager(const QString &path) +{ + m_db = QSqlDatabase::addDatabase("QSQLITE"); + m_db.setDatabaseName(path); + + if (!m_db.open()) + { + qDebug() << "Error: connection with database fail"; + } + else + { + qDebug() << "Database: connection ok"; + } +} + +bool DbManager::addPerson(const QString& name) +{ + bool success = false; + + if (!name.isEmpty()) + { + QSqlQuery queryAdd; + queryAdd.prepare("INSERT INTO people (name) VALUES (:name)"); + queryAdd.bindValue(":name", name); + + if(queryAdd.exec()) + { + success = true; + } + else + { + qDebug() << "add person failed: " << queryAdd.lastError(); + } + } + else + { + qDebug() << "add person failed: name cannot be empty"; + } + + return success; +} + +bool DbManager::removePerson(const QString& name) +{ + bool success = false; + + if (personExists(name)) + { + QSqlQuery queryDelete; + queryDelete.prepare("DELETE FROM people WHERE name = (:name)"); + queryDelete.bindValue(":name", name); + success = queryDelete.exec(); + + if(!success) + { + qDebug() << "remove person failed: " << queryDelete.lastError(); + } + } + else + { + qDebug() << "remove person failed: person doesnt exist"; + } + + return success; +} + +void DbManager::printAllPersons() const +{ + qDebug() << "Persons in db:"; + QSqlQuery query("SELECT * FROM people"); + int idName = query.record().indexOf("name"); + while (query.next()) + { + QString name = query.value(idName).toString(); + qDebug() << "===" << name; + } +} + +bool DbManager::personExists(const QString& name) const +{ + bool exists = false; + + QSqlQuery checkQuery; + checkQuery.prepare("SELECT name FROM people WHERE name = (:name)"); + checkQuery.bindValue(":name", name); + + if (checkQuery.exec()) + { + if (checkQuery.next()) + { + exists = true; + } + } + else + { + qDebug() << "person exists failed: " << checkQuery.lastError(); + } + + return exists; +} + +bool DbManager::removeAllPersons() +{ + bool success = false; + + QSqlQuery removeQuery; + removeQuery.prepare("DELETE FROM people"); + + if (removeQuery.exec()) + { + success = true; + } + else + { + qDebug() << "remove all persons failed: " << removeQuery.lastError(); + } + + return success; +} diff --git a/dbmanager.h b/dbmanager.h new file mode 100644 index 0000000..cbc14ed --- /dev/null +++ b/dbmanager.h @@ -0,0 +1,20 @@ +#ifndef DBMANAGER_H +#define DBMANAGER_H + +#include + +class DbManager +{ +public: + DbManager(const QString& path); + bool addPerson(const QString& name); + bool removePerson(const QString& name); + bool personExists(const QString& name) const; + void printAllPersons() const; + bool removeAllPersons(); + +private: + QSqlDatabase m_db; +}; + +#endif // DBMANAGER_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..a75b0dd --- /dev/null +++ b/main.cpp @@ -0,0 +1,20 @@ +#include +#include + +// Set valid db path before first run +static const QString path = "/path/people.db"; + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + DbManager db(path); + db.addPerson("A"); + db.addPerson("B"); + db.addPerson("C"); + db.printAllPersons(); + db.removePerson("C"); + db.printAllPersons(); + db.removeAllPersons(); + return a.exec(); +} diff --git a/sqlite_qt.pro b/sqlite_qt.pro new file mode 100644 index 0000000..d64faf7 --- /dev/null +++ b/sqlite_qt.pro @@ -0,0 +1,22 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2015-08-27T18:41:32 +# +#------------------------------------------------- + +QT += core sql + +QT -= gui + +TARGET = sqlite_qt +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + + +SOURCES += main.cpp \ + dbmanager.cpp + +HEADERS += \ + dbmanager.h