-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |