1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: initdb.h Example File (demos/books/initdb.h)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
18
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">initdb.h Example File<br /><small><small>demos/books/initdb.h</small></small></h1>
20
<pre> #ifndef INITDB_H
23
#include <QtSql>
25
void addBook(QSqlQuery &q, const QString &title, int year, const QVariant &authorId,
26
const QVariant &genreId, int rating)
28
q.addBindValue(title);
30
q.addBindValue(authorId);
31
q.addBindValue(genreId);
32
q.addBindValue(rating);
36
QVariant addGenre(QSqlQuery &q, const QString &name)
40
return q.lastInsertId();
43
QVariant addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate)
46
q.addBindValue(birthdate);
48
return q.lastInsertId();
53
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
54
db.setDatabaseName(":memory:");
57
return db.lastError();
59
QStringList tables = db.tables();
60
if (tables.contains("books", Qt::CaseInsensitive)
61
&& tables.contains("authors", Qt::CaseInsensitive))
65
if (!q.exec(QLatin1String("create table books(id integer primary key, title varchar, author integer, genre integer, year integer, rating integer)")))
67
if (!q.exec(QLatin1String("create table authors(id integer primary key, name varchar, birthdate date)")))
69
if (!q.exec(QLatin1String("create table genres(id integer primary key, name varchar)")))
72
if (!q.prepare(QLatin1String("insert into authors(name, birthdate) values(?, ?)")))
74
QVariant asimovId = addAuthor(q, QLatin1String("Isaac Asimov"), QDate(1920, 2, 1));
75
QVariant greeneId = addAuthor(q, QLatin1String("Graham Greene"), QDate(1904, 10, 2));
76
QVariant pratchettId = addAuthor(q, QLatin1String("Terry Pratchett"), QDate(1948, 4, 28));
78
if (!q.prepare(QLatin1String("insert into genres(name) values(?)")))
80
QVariant sfiction = addGenre(q, QLatin1String("Science Fiction"));
81
QVariant fiction = addGenre(q, QLatin1String("Fiction"));
82
QVariant fantasy = addGenre(q, QLatin1String("Fantasy"));
84
if (!q.prepare(QLatin1String("insert into books(title, year, author, genre, rating) values(?, ?, ?, ?, ?)")))
86
addBook(q, QLatin1String("Foundation"), 1951, asimovId, sfiction, 3);
87
addBook(q, QLatin1String("Foundation and Empire"), 1952, asimovId, sfiction, 4);
88
addBook(q, QLatin1String("Second Foundation"), 1953, asimovId, sfiction, 3);
89
addBook(q, QLatin1String("Foundation's Edge"), 1982, asimovId, sfiction, 3);
90
addBook(q, QLatin1String("Foundation and Earth"), 1986, asimovId, sfiction, 4);
91
addBook(q, QLatin1String("Prelude to Foundation"), 1988, asimovId, sfiction, 3);
92
addBook(q, QLatin1String("Forward the Foundation"), 1993, asimovId, sfiction, 3);
93
addBook(q, QLatin1String("The Power and the Glory"), 1940, greeneId, fiction, 4);
94
addBook(q, QLatin1String("The Third Man"), 1950, greeneId, fiction, 5);
95
addBook(q, QLatin1String("Our Man in Havana"), 1958, greeneId, fiction, 4);
96
addBook(q, QLatin1String("Guards! Guards!"), 1989, pratchettId, fantasy, 3);
97
addBook(q, QLatin1String("Night Watch"), 2002, pratchettId, fantasy, 3);
98
addBook(q, QLatin1String("Going Postal"), 2004, pratchettId, fantasy, 3);
104
<p /><address><hr /><div align="center">
105
<table width="100%" cellspacing="0" border="0"><tr class="address">
106
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
107
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
108
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
109
</tr></table></div></address></body>