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

« back to all changes in this revision

Viewing changes to doc/html/demos-books-initdb-h.html

  • 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
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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">
 
5
<head>
 
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>
 
13
</head>
 
14
<body>
 
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
16
<tr>
 
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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <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>&nbsp;   #ifndef INITDB_H
 
21
    #define INITDB_H
 
22
 
 
23
    #include &lt;QtSql&gt;
 
24
 
 
25
    void addBook(QSqlQuery &amp;q, const QString &amp;title, int year, const QVariant &amp;authorId,
 
26
                 const QVariant &amp;genreId, int rating)
 
27
    {
 
28
        q.addBindValue(title);
 
29
        q.addBindValue(year);
 
30
        q.addBindValue(authorId);
 
31
        q.addBindValue(genreId);
 
32
        q.addBindValue(rating);
 
33
        q.exec();
 
34
    }
 
35
 
 
36
    QVariant addGenre(QSqlQuery &amp;q, const QString &amp;name)
 
37
    {
 
38
        q.addBindValue(name);
 
39
        q.exec();
 
40
        return q.lastInsertId();
 
41
    }
 
42
 
 
43
    QVariant addAuthor(QSqlQuery &amp;q, const QString &amp;name, const QDate &amp;birthdate)
 
44
    {
 
45
        q.addBindValue(name);
 
46
        q.addBindValue(birthdate);
 
47
        q.exec();
 
48
        return q.lastInsertId();
 
49
    }
 
50
 
 
51
    QSqlError initDb()
 
52
    {
 
53
        QSqlDatabase db = QSqlDatabase::addDatabase(&quot;QSQLITE&quot;);
 
54
        db.setDatabaseName(&quot;:memory:&quot;);
 
55
 
 
56
        if (!db.open())
 
57
            return db.lastError();
 
58
 
 
59
        QStringList tables = db.tables();
 
60
        if (tables.contains(&quot;books&quot;, Qt::CaseInsensitive)
 
61
            &amp;&amp; tables.contains(&quot;authors&quot;, Qt::CaseInsensitive))
 
62
            return QSqlError();
 
63
 
 
64
        QSqlQuery q;
 
65
        if (!q.exec(QLatin1String(&quot;create table books(id integer primary key, title varchar, author integer, genre integer, year integer, rating integer)&quot;)))
 
66
            return q.lastError();
 
67
        if (!q.exec(QLatin1String(&quot;create table authors(id integer primary key, name varchar, birthdate date)&quot;)))
 
68
            return q.lastError();
 
69
        if (!q.exec(QLatin1String(&quot;create table genres(id integer primary key, name varchar)&quot;)))
 
70
            return q.lastError();
 
71
 
 
72
        if (!q.prepare(QLatin1String(&quot;insert into authors(name, birthdate) values(?, ?)&quot;)))
 
73
            return q.lastError();
 
74
        QVariant asimovId = addAuthor(q, QLatin1String(&quot;Isaac Asimov&quot;), QDate(1920, 2, 1));
 
75
        QVariant greeneId = addAuthor(q, QLatin1String(&quot;Graham Greene&quot;), QDate(1904, 10, 2));
 
76
        QVariant pratchettId = addAuthor(q, QLatin1String(&quot;Terry Pratchett&quot;), QDate(1948, 4, 28));
 
77
 
 
78
        if (!q.prepare(QLatin1String(&quot;insert into genres(name) values(?)&quot;)))
 
79
            return q.lastError();
 
80
        QVariant sfiction = addGenre(q, QLatin1String(&quot;Science Fiction&quot;));
 
81
        QVariant fiction = addGenre(q, QLatin1String(&quot;Fiction&quot;));
 
82
        QVariant fantasy = addGenre(q, QLatin1String(&quot;Fantasy&quot;));
 
83
 
 
84
        if (!q.prepare(QLatin1String(&quot;insert into books(title, year, author, genre, rating) values(?, ?, ?, ?, ?)&quot;)))
 
85
            return q.lastError();
 
86
        addBook(q, QLatin1String(&quot;Foundation&quot;), 1951, asimovId, sfiction, 3);
 
87
        addBook(q, QLatin1String(&quot;Foundation and Empire&quot;), 1952, asimovId, sfiction, 4);
 
88
        addBook(q, QLatin1String(&quot;Second Foundation&quot;), 1953, asimovId, sfiction, 3);
 
89
        addBook(q, QLatin1String(&quot;Foundation's Edge&quot;), 1982, asimovId, sfiction, 3);
 
90
        addBook(q, QLatin1String(&quot;Foundation and Earth&quot;), 1986, asimovId, sfiction, 4);
 
91
        addBook(q, QLatin1String(&quot;Prelude to Foundation&quot;), 1988, asimovId, sfiction, 3);
 
92
        addBook(q, QLatin1String(&quot;Forward the Foundation&quot;), 1993, asimovId, sfiction, 3);
 
93
        addBook(q, QLatin1String(&quot;The Power and the Glory&quot;), 1940, greeneId, fiction, 4);
 
94
        addBook(q, QLatin1String(&quot;The Third Man&quot;), 1950, greeneId, fiction, 5);
 
95
        addBook(q, QLatin1String(&quot;Our Man in Havana&quot;), 1958, greeneId, fiction, 4);
 
96
        addBook(q, QLatin1String(&quot;Guards! Guards!&quot;), 1989, pratchettId, fantasy, 3);
 
97
        addBook(q, QLatin1String(&quot;Night Watch&quot;), 2002, pratchettId, fantasy, 3);
 
98
        addBook(q, QLatin1String(&quot;Going Postal&quot;), 2004, pratchettId, fantasy, 3);
 
99
 
 
100
        return QSqlError();
 
101
    }
 
102
 
 
103
    #endif</pre>
 
104
<p /><address><hr /><div align="center">
 
105
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
106
<td width="30%">Copyright &copy; 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>
 
110
</html>