author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the demonstration applications of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#ifndef INITDB_H |
|
43 |
#define INITDB_H |
|
44 |
||
45 |
#include <QtSql> |
|
46 |
||
47 |
void 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 |
||
58 |
QVariant addGenre(QSqlQuery &q, const QString &name) |
|
59 |
{ |
|
60 |
q.addBindValue(name); |
|
61 |
q.exec(); |
|
62 |
return q.lastInsertId(); |
|
63 |
} |
|
64 |
||
65 |
QVariant 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 |
||
73 |
QSqlError 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 |