diff options
author | Mateusz Kaduk <mateusz.kaduk@gmail.com> | 2005-05-22 09:54:55 +0000 |
---|---|---|
committer | Mateusz Kaduk <mateusz.kaduk@gmail.com> | 2005-05-22 09:54:55 +0000 |
commit | 795182f6ad0afd57b5490b9fa5417222b2118788 (patch) | |
tree | e617993efea739c18623d42098d5eafd48e2cdb4 /src | |
parent | 198d4aac190c28ab245fb979336c06cb0c51776c (diff) | |
download | manaserv-795182f6ad0afd57b5490b9fa5417222b2118788.tar.gz manaserv-795182f6ad0afd57b5490b9fa5417222b2118788.tar.bz2 manaserv-795182f6ad0afd57b5490b9fa5417222b2118788.tar.xz manaserv-795182f6ad0afd57b5490b9fa5417222b2118788.zip |
Adding default tables if sqlite_master table is empty
Diffstat (limited to 'src')
-rw-r--r-- | src/account.h | 2 | ||||
-rw-r--r-- | src/main.cpp | 36 |
2 files changed, 32 insertions, 6 deletions
diff --git a/src/account.h b/src/account.h index f33bee75..a8b8759c 100644 --- a/src/account.h +++ b/src/account.h @@ -26,7 +26,7 @@ #include <iostream> #include "object.h" -#include "sqlite/SQLiteWrapper.h" +#include "main.h" #define ACC_MAX_CHARS 4 diff --git a/src/main.cpp b/src/main.cpp index afcfe239..87f7d6d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -67,6 +67,30 @@ Skill skillTree("base"); /**< Skill tree */ SQLiteWrapper sqlite; /**< Database */ /** + * Create tables if necessary + */ +void create_tables_if_necessary() +{ + SQLiteWrapper::ResultTable r; + + if (!sqlite.SelectStmt("select count(*) from sqlite_master where tbl_name='topics' and type='table'", r)) { + std::cout << "Error with select count(*) [create_tables_if_necessary]" << sqlite.LastError().c_str() << std::endl; + } + + if (r.records_[0].fields_[0] != "0") + return; + + sqlite.Begin(); + if (sqlite.DirectStatement("create table tmw_accounts(user TEXT, password TEXT, email TEXT)")) { + std::cout << "Database: table tmw_accounts created" << std::endl; + } + else { + std::cout << "Database: table exist" << std::endl; + } + sqlite.Commit(); +} + +/** * SDL timer callback, sends a <code>TMW_WORLD_TICK</code> event. */ Uint32 worldTick(Uint32 interval, void *param) @@ -116,11 +140,14 @@ void initialize() // Open database if (sqlite.Open("tmw.db")) { - std::cout << "tmw.db created or opened" << std::endl; + std::cout << "Database: tmw.db created or opened" << std::endl; } else { - std::cout << "couldn't open tmw.db" << std::endl; + std::cout << "Database: couldn't open tmw.db" << std::endl; } + + //Create tables + create_tables_if_necessary(); } /** @@ -142,14 +169,13 @@ void deinitialize() // Close database if (sqlite.Close()) { - std::cout << "tmw.db closed" << std::endl; + std::cout << "Database: tmw.db closed" << std::endl; } else { - std::cout << "couldn't close tmw.db" << std::endl; + std::cout << "Database: couldn't close tmw.db" << std::endl; } } - /** * Update game world */ |