Skip to content

Commit

Permalink
Database manager added
Browse files Browse the repository at this point in the history
  • Loading branch information
katecpp committed Aug 28, 2015
1 parent 1641541 commit 8b05e45
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
125 changes: 125 additions & 0 deletions dbmanager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <dbmanager.h>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlRecord>
#include <QDebug>

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;
}
20 changes: 20 additions & 0 deletions dbmanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef DBMANAGER_H
#define DBMANAGER_H

#include <QSqlDatabase>

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
20 changes: 20 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <QCoreApplication>
#include <dbmanager.h>

// 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();
}
22 changes: 22 additions & 0 deletions sqlite_qt.pro
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8b05e45

Please sign in to comment.