demos/books/initdb.h File Reference

#include <QtSql>

Include dependency graph for initdb.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void addBook (QSqlQuery &q, const QString &title, int year, const QVariant &authorId, const QVariant &genreId, int rating)
QVariant addGenre (QSqlQuery &q, const QString &name)
QVariant addAuthor (QSqlQuery &q, const QString &name, const QDate &birthdate)
QSqlError initDb ()


Function Documentation

QVariant addAuthor ( QSqlQuery q,
const QString name,
const QDate birthdate 
)

Definition at line 47 of file initdb.h.

References QSqlQuery::addBindValue(), QSqlQuery::exec(), QSqlQuery::lastInsertId(), and name.

Referenced by initDb().

00048 {
00049     q.addBindValue(name);
00050     q.addBindValue(birthdate);
00051     q.exec();
00052     return q.lastInsertId();
00053 }

Here is the call graph for this function:

void addBook ( QSqlQuery q,
const QString title,
int  year,
const QVariant authorId,
const QVariant genreId,
int  rating 
)

Definition at line 29 of file initdb.h.

References QSqlQuery::addBindValue(), and QSqlQuery::exec().

Referenced by initDb().

00031 {
00032     q.addBindValue(title);
00033     q.addBindValue(year);
00034     q.addBindValue(authorId);
00035     q.addBindValue(genreId);
00036     q.addBindValue(rating);
00037     q.exec();
00038 }

Here is the call graph for this function:

QVariant addGenre ( QSqlQuery q,
const QString name 
)

Definition at line 40 of file initdb.h.

References QSqlQuery::addBindValue(), QSqlQuery::exec(), QSqlQuery::lastInsertId(), and name.

Referenced by initDb().

00041 {
00042     q.addBindValue(name);
00043     q.exec();
00044     return q.lastInsertId();
00045 }

Here is the call graph for this function:

QSqlError initDb (  ) 

Definition at line 55 of file initdb.h.

References addAuthor(), addBook(), QSqlDatabase::addDatabase(), addGenre(), Qt::CaseInsensitive, QStringList::contains(), QSqlQuery::exec(), QSqlDatabase::lastError(), QSqlQuery::lastError(), QSqlDatabase::open(), QSqlQuery::prepare(), QSqlDatabase::setDatabaseName(), and QSqlDatabase::tables().

Referenced by BookWindow::BookWindow().

00056 {
00057     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
00058     db.setDatabaseName(":memory:");
00059 
00060     if (!db.open())
00061         return db.lastError();
00062 
00063     QStringList tables = db.tables();
00064     if (tables.contains("books", Qt::CaseInsensitive)
00065         && tables.contains("authors", Qt::CaseInsensitive))
00066         return QSqlError();
00067 
00068     QSqlQuery q;
00069     if (!q.exec(QLatin1String("create table books(id integer primary key, title varchar, author integer, genre integer, year integer, rating integer)")))
00070         return q.lastError();
00071     if (!q.exec(QLatin1String("create table authors(id integer primary key, name varchar, birthdate date)")))
00072         return q.lastError();
00073     if (!q.exec(QLatin1String("create table genres(id integer primary key, name varchar)")))
00074         return q.lastError();
00075 
00076     if (!q.prepare(QLatin1String("insert into authors(name, birthdate) values(?, ?)")))
00077         return q.lastError();
00078     QVariant asimovId = addAuthor(q, QLatin1String("Isaac Asimov"), QDate(1920, 2, 1));
00079     QVariant greeneId = addAuthor(q, QLatin1String("Graham Greene"), QDate(1904, 10, 2));
00080     QVariant pratchettId = addAuthor(q, QLatin1String("Terry Pratchett"), QDate(1948, 4, 28));
00081 
00082     if (!q.prepare(QLatin1String("insert into genres(name) values(?)")))
00083         return q.lastError();
00084     QVariant sfiction = addGenre(q, QLatin1String("Science Fiction"));
00085     QVariant fiction = addGenre(q, QLatin1String("Fiction"));
00086     QVariant fantasy = addGenre(q, QLatin1String("Fantasy"));
00087 
00088     if (!q.prepare(QLatin1String("insert into books(title, year, author, genre, rating) values(?, ?, ?, ?, ?)")))
00089         return q.lastError();
00090     addBook(q, QLatin1String("Foundation"), 1951, asimovId, sfiction, 3);
00091     addBook(q, QLatin1String("Foundation and Empire"), 1952, asimovId, sfiction, 4);
00092     addBook(q, QLatin1String("Second Foundation"), 1953, asimovId, sfiction, 3);
00093     addBook(q, QLatin1String("Foundation's Edge"), 1982, asimovId, sfiction, 3);
00094     addBook(q, QLatin1String("Foundation and Earth"), 1986, asimovId, sfiction, 4);
00095     addBook(q, QLatin1String("Prelude to Foundation"), 1988, asimovId, sfiction, 3);
00096     addBook(q, QLatin1String("Forward the Foundation"), 1993, asimovId, sfiction, 3);
00097     addBook(q, QLatin1String("The Power and the Glory"), 1940, greeneId, fiction, 4);
00098     addBook(q, QLatin1String("The Third Man"), 1950, greeneId, fiction, 5);
00099     addBook(q, QLatin1String("Our Man in Havana"), 1958, greeneId, fiction, 4);
00100     addBook(q, QLatin1String("Guards! Guards!"), 1989, pratchettId, fantasy, 3);
00101     addBook(q, QLatin1String("Night Watch"), 2002, pratchettId, fantasy, 3);
00102     addBook(q, QLatin1String("Going Postal"), 2004, pratchettId, fantasy, 3);
00103 
00104     return QSqlError();
00105 }

Here is the call graph for this function:


Generated on Thu Mar 15 12:03:48 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1