9 BookDb *BookDb::instance()
12 theInstance = new BookDb;
28 bool shouldCreate = false;
29 QFileInfo info(Platform::dbPath());
32 if (!dbDir.mkpath(info.absolutePath())) {
33 qCritical() << "Could not create" << info.absolutePath();
37 db = QSqlDatabase::addDatabase("QSQLITE");
38 db.setDatabaseName(QDir::toNativeSeparators(Platform::dbPath()));
40 qCritical() << "Could not open" << Platform::dbPath() << ": Error"
41 << db.lastError().text();
57 if (!query.exec("create table book "
58 "(name text primary key, content blob)")) {
59 qCritical() << "Failed to create database:"
60 << query.lastError().text();
64 QVariantHash BookDb::load(const QString &book)
70 QSqlQuery query("select content from book where name = ?");
71 query.bindValue(0, book);
72 query.setForwardOnly(true);
74 qCritical() << "Query failed:" << query.lastError().text();
77 while (query.next()) {
78 bytes = query.value(0).toByteArray();
79 QDataStream in(bytes);
86 void BookDb::save(const QString &book, const QVariantHash &data)
91 QDataStream out(&bytes, QIODevice::WriteOnly);
93 QSqlQuery query("insert or replace into book values (?, ?)");
94 query.bindValue(0, book);
95 query.bindValue(1, bytes);
97 qCritical() << "Query failed:" << query.lastError().text();
101 void BookDb::remove(const QString &book)
105 QSqlQuery query("delete from book where name = ?");
106 query.bindValue(0, book);
108 qCritical() << "Query failed:" << query.lastError().text();
112 QStringList BookDb::books()
116 QSqlQuery query("select name from book");
117 query.setForwardOnly(true);
119 qCritical() << "Query failed:" << query.lastError().text();
122 while (query.next()) {
123 ret.append(query.value(0).toString());
129 void BookDb::removeAll()
131 foreach (QString book, books()) {