~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to demos/books/initdb.h

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef INITDB_H
 
2
#define INITDB_H
 
3
 
 
4
#include <QtSql>
 
5
 
 
6
void addBook(QSqlQuery &q, const QString &title, int year, const QVariant &authorId,
 
7
             const QVariant &genreId, int rating)
 
8
{
 
9
    q.addBindValue(title);
 
10
    q.addBindValue(year);
 
11
    q.addBindValue(authorId);
 
12
    q.addBindValue(genreId);
 
13
    q.addBindValue(rating);
 
14
    q.exec();
 
15
}
 
16
 
 
17
QVariant addGenre(QSqlQuery &q, const QString &name)
 
18
{
 
19
    q.addBindValue(name);
 
20
    q.exec();
 
21
    return q.lastInsertId();
 
22
}
 
23
 
 
24
QVariant addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate)
 
25
{
 
26
    q.addBindValue(name);
 
27
    q.addBindValue(birthdate);
 
28
    q.exec();
 
29
    return q.lastInsertId();
 
30
}
 
31
 
 
32
QSqlError initDb()
 
33
{
 
34
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
 
35
    db.setDatabaseName(":memory:");
 
36
 
 
37
    if (!db.open())
 
38
        return db.lastError();
 
39
 
 
40
    QStringList tables = db.tables();
 
41
    if (tables.contains("books", Qt::CaseInsensitive)
 
42
        && tables.contains("authors", Qt::CaseInsensitive))
 
43
        return QSqlError();
 
44
 
 
45
    QSqlQuery q;
 
46
    if (!q.exec(QLatin1String("create table books(id integer primary key, title varchar, author integer, genre integer, year integer, rating integer)")))
 
47
        return q.lastError();
 
48
    if (!q.exec(QLatin1String("create table authors(id integer primary key, name varchar, birthdate date)")))
 
49
        return q.lastError();
 
50
    if (!q.exec(QLatin1String("create table genres(id integer primary key, name varchar)")))
 
51
        return q.lastError();
 
52
 
 
53
    if (!q.prepare(QLatin1String("insert into authors(name, birthdate) values(?, ?)")))
 
54
        return q.lastError();
 
55
    QVariant asimovId = addAuthor(q, QLatin1String("Isaac Asimov"), QDate(1920, 2, 1));
 
56
    QVariant greeneId = addAuthor(q, QLatin1String("Graham Greene"), QDate(1904, 10, 2));
 
57
    QVariant pratchettId = addAuthor(q, QLatin1String("Terry Pratchett"), QDate(1948, 4, 28));
 
58
 
 
59
    if (!q.prepare(QLatin1String("insert into genres(name) values(?)")))
 
60
        return q.lastError();
 
61
    QVariant sfiction = addGenre(q, QLatin1String("Science Fiction"));
 
62
    QVariant fiction = addGenre(q, QLatin1String("Fiction"));
 
63
    QVariant fantasy = addGenre(q, QLatin1String("Fantasy"));
 
64
 
 
65
    if (!q.prepare(QLatin1String("insert into books(title, year, author, genre, rating) values(?, ?, ?, ?, ?)")))
 
66
        return q.lastError();
 
67
    addBook(q, QLatin1String("Foundation"), 1951, asimovId, sfiction, 3);
 
68
    addBook(q, QLatin1String("Foundation and Empire"), 1952, asimovId, sfiction, 4);
 
69
    addBook(q, QLatin1String("Second Foundation"), 1953, asimovId, sfiction, 3);
 
70
    addBook(q, QLatin1String("Foundation's Edge"), 1982, asimovId, sfiction, 3);
 
71
    addBook(q, QLatin1String("Foundation and Earth"), 1986, asimovId, sfiction, 4);
 
72
    addBook(q, QLatin1String("Prelude to Foundation"), 1988, asimovId, sfiction, 3);
 
73
    addBook(q, QLatin1String("Forward the Foundation"), 1993, asimovId, sfiction, 3);
 
74
    addBook(q, QLatin1String("The Power and the Glory"), 1940, greeneId, fiction, 4);
 
75
    addBook(q, QLatin1String("The Third Man"), 1950, greeneId, fiction, 5);
 
76
    addBook(q, QLatin1String("Our Man in Havana"), 1958, greeneId, fiction, 4);
 
77
    addBook(q, QLatin1String("Guards! Guards!"), 1989, pratchettId, fantasy, 3);
 
78
    addBook(q, QLatin1String("Night Watch"), 2002, pratchettId, fantasy, 3);
 
79
    addBook(q, QLatin1String("Going Postal"), 2004, pratchettId, fantasy, 3);
 
80
 
 
81
    return QSqlError();
 
82
}
 
83
 
 
84
#endif
 
85