demos/books/initdb.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the demonstration applications of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #ifndef INITDB_H
00025 #define INITDB_H
00026 
00027 #include <QtSql>
00028 
00029 void addBook(QSqlQuery &q, const QString &title, int year, const QVariant &authorId,
00030              const QVariant &genreId, int rating)
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 }
00039 
00040 QVariant addGenre(QSqlQuery &q, const QString &name)
00041 {
00042     q.addBindValue(name);
00043     q.exec();
00044     return q.lastInsertId();
00045 }
00046 
00047 QVariant addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate)
00048 {
00049     q.addBindValue(name);
00050     q.addBindValue(birthdate);
00051     q.exec();
00052     return q.lastInsertId();
00053 }
00054 
00055 QSqlError initDb()
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 }
00106 
00107 #endif

Generated on Thu Mar 15 11:52:28 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1