2 * This file is part of Jenirok.
4 * Jenirok is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Jenirok is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Jenirok. If not, see <http://www.gnu.org/licenses/>.
19 #include <QtCore/QDir>
20 #include <QtCore/QDebug>
21 #include <QtSql/QSqlQuery>
22 #include <QtSql/QSqlError>
27 const QString SQL_DRIVER = "QSQLITE";
28 const QString SQL_DATABASE = ".jenirok.db";
31 bool DB::initialized_ = false;
38 db_ = QSqlDatabase::addDatabase(SQL_DRIVER);
39 QString path = QDir::home().path() + QDir::separator() + SQL_DATABASE;
40 db_.setDatabaseName(QDir::toNativeSeparators(path));
43 bool ret = db_.open();
47 QSqlError error = db_.lastError();
48 qDebug() << error.text();
51 if(ret && !initialized_)
53 QSqlQuery query("SELECT value FROM settings WHERE name = 'initialized'");
74 void DB::removeDatabase()
78 QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
87 QSqlDatabase& DB::instance()
92 bool DB::createTables()
98 ret = ret && query.exec("CREATE TABLE cache (id INTEGER PRIMARY KEY, number VARCHAR(32) NOT NULL UNIQUE, name VARCHAR(255) NOT NULL, street VARCHAR(255) NOT NULL, city VARCHAR(255) NOT NULL)");
99 ret = ret && query.exec("CREATE TABLE settings (name VARCHAR(255) NOT NULL PRIMARY KEY, value VARCHAR(255) NOT NULL)");
100 ret = ret && query.exec("INSERT INTO settings(name, value) VALUES('initialized', '1')");