Skip to content

Commit

Permalink
Added the ability to create table and a database from the code withou…
Browse files Browse the repository at this point in the history
…t needing to do so from the terminal.
  • Loading branch information
logicxd committed Oct 29, 2016
1 parent 65d44a2 commit 5f77ece
Show file tree
Hide file tree
Showing 4 changed files with 361 additions and 1 deletion.
16 changes: 16 additions & 0 deletions dbmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ bool DbManager::isOpen() const
return m_db.isOpen();
}

bool DbManager::createTable()
{
bool success = false;

QSqlQuery query;
query.prepare("CREATE TABLE people(id INTEGER PRIMARY KEY, name TEXT);");

if (!query.exec())
{
qDebug() << "Couldn't create the table 'people': one might already exist.";
success = false;
}

return success;
}

bool DbManager::addPerson(const QString& name)
{
bool success = false;
Expand Down
6 changes: 6 additions & 0 deletions dbmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class DbManager

bool isOpen() const;

/**
* @brief Creates a new 'people' table if it doesn't already exist
* @return true - 'people' table created successfully, false - table not created
*/
bool createTable();

/**
* @brief Add person data to db
* @param name - name of person to add
Expand Down
3 changes: 2 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <dbmanager.h>

// Set valid db path before first run
static const QString path = "/path/people.db";
static const QString path = "example.db";

int main(int argc, char *argv[])
{
Expand All @@ -13,6 +13,7 @@ int main(int argc, char *argv[])

if (db.isOpen())
{
db.createTable();
db.addPerson("A");
db.addPerson("B");
db.addPerson("C");
Expand Down
Loading

0 comments on commit 5f77ece

Please sign in to comment.