7 BookDb *BookDb::instance()
10 theInstance = new BookDb;
26 bool shouldCreate = false;
27 QFileInfo info(Platform::instance()->dbPath());
30 if (!dbDir.mkpath(info.absolutePath())) {
31 qCritical() << "Could not create" << info.absolutePath();
35 db = QSqlDatabase::addDatabase("QSQLITE");
36 db.setDatabaseName(QDir::toNativeSeparators(Platform::instance()->dbPath()));
38 qCritical() << "Could not open" << Platform::instance()->dbPath()
39 << ": Error" << db.lastError().text();
55 if (!query.exec("create table book "
56 "(name text primary key, content blob)")) {
57 qCritical() << "Failed to create database:"
58 << query.lastError().text();
62 QVariantHash BookDb::load(const QString &book)
68 QSqlQuery query("select content from book where name = ?");
69 query.bindValue(0, book);
70 query.setForwardOnly(true);
72 qCritical() << "Query failed:" << query.lastError().text();
75 while (query.next()) {
76 bytes = query.value(0).toByteArray();
77 QDataStream in(bytes);
84 void BookDb::save(const QString &book, const QVariantHash &data)
89 QDataStream out(&bytes, QIODevice::WriteOnly);
91 QSqlQuery query("insert or replace into book values (?, ?)");
92 query.bindValue(0, book);
93 query.bindValue(1, bytes);
95 qCritical() << "Query failed:" << query.lastError().text();
99 void BookDb::remove(const QString &book)
103 QSqlQuery query("delete from book where name = ?");
104 query.bindValue(0, book);
106 qCritical() << "Query failed:" << query.lastError().text();
110 QStringList BookDb::books()
114 QSqlQuery query("select name from book");
115 query.setForwardOnly(true);
117 qCritical() << "Query failed:" << query.lastError().text();
120 while (query.next()) {
121 ret.append(query.value(0).toString());
127 void BookDb::removeAll()
129 foreach (QString book, books()) {