source: trunk/demos/books/initdb.h@ 158

Last change on this file since 158 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef INITDB_H
43#define INITDB_H
44
45#include <QtSql>
46
47void addBook(QSqlQuery &q, const QString &title, int year, const QVariant &authorId,
48 const QVariant &genreId, int rating)
49{
50 q.addBindValue(title);
51 q.addBindValue(year);
52 q.addBindValue(authorId);
53 q.addBindValue(genreId);
54 q.addBindValue(rating);
55 q.exec();
56}
57
58QVariant addGenre(QSqlQuery &q, const QString &name)
59{
60 q.addBindValue(name);
61 q.exec();
62 return q.lastInsertId();
63}
64
65QVariant addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate)
66{
67 q.addBindValue(name);
68 q.addBindValue(birthdate);
69 q.exec();
70 return q.lastInsertId();
71}
72
73QSqlError initDb()
74{
75 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
76 db.setDatabaseName(":memory:");
77
78 if (!db.open())
79 return db.lastError();
80
81 QStringList tables = db.tables();
82 if (tables.contains("books", Qt::CaseInsensitive)
83 && tables.contains("authors", Qt::CaseInsensitive))
84 return QSqlError();
85
86 QSqlQuery q;
87 if (!q.exec(QLatin1String("create table books(id integer primary key, title varchar, author integer, genre integer, year integer, rating integer)")))
88 return q.lastError();
89 if (!q.exec(QLatin1String("create table authors(id integer primary key, name varchar, birthdate date)")))
90 return q.lastError();
91 if (!q.exec(QLatin1String("create table genres(id integer primary key, name varchar)")))
92 return q.lastError();
93
94 if (!q.prepare(QLatin1String("insert into authors(name, birthdate) values(?, ?)")))
95 return q.lastError();
96 QVariant asimovId = addAuthor(q, QLatin1String("Isaac Asimov"), QDate(1920, 2, 1));
97 QVariant greeneId = addAuthor(q, QLatin1String("Graham Greene"), QDate(1904, 10, 2));
98 QVariant pratchettId = addAuthor(q, QLatin1String("Terry Pratchett"), QDate(1948, 4, 28));
99
100 if (!q.prepare(QLatin1String("insert into genres(name) values(?)")))
101 return q.lastError();
102 QVariant sfiction = addGenre(q, QLatin1String("Science Fiction"));
103 QVariant fiction = addGenre(q, QLatin1String("Fiction"));
104 QVariant fantasy = addGenre(q, QLatin1String("Fantasy"));
105
106 if (!q.prepare(QLatin1String("insert into books(title, year, author, genre, rating) values(?, ?, ?, ?, ?)")))
107 return q.lastError();
108 addBook(q, QLatin1String("Foundation"), 1951, asimovId, sfiction, 3);
109 addBook(q, QLatin1String("Foundation and Empire"), 1952, asimovId, sfiction, 4);
110 addBook(q, QLatin1String("Second Foundation"), 1953, asimovId, sfiction, 3);
111 addBook(q, QLatin1String("Foundation's Edge"), 1982, asimovId, sfiction, 3);
112 addBook(q, QLatin1String("Foundation and Earth"), 1986, asimovId, sfiction, 4);
113 addBook(q, QLatin1String("Prelude to Foundation"), 1988, asimovId, sfiction, 3);
114 addBook(q, QLatin1String("Forward the Foundation"), 1993, asimovId, sfiction, 3);
115 addBook(q, QLatin1String("The Power and the Glory"), 1940, greeneId, fiction, 4);
116 addBook(q, QLatin1String("The Third Man"), 1950, greeneId, fiction, 5);
117 addBook(q, QLatin1String("Our Man in Havana"), 1958, greeneId, fiction, 4);
118 addBook(q, QLatin1String("Guards! Guards!"), 1989, pratchettId, fantasy, 3);
119 addBook(q, QLatin1String("Night Watch"), 2002, pratchettId, fantasy, 3);
120 addBook(q, QLatin1String("Going Postal"), 2004, pratchettId, fantasy, 3);
121
122 return QSqlError();
123}
124
125#endif
Note: See TracBrowser for help on using the repository browser.