| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$ | 
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows: | 
|---|
| 11 | ** | 
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without | 
|---|
| 13 | ** modification, are permitted provided that the following conditions are | 
|---|
| 14 | ** met: | 
|---|
| 15 | **   * Redistributions of source code must retain the above copyright | 
|---|
| 16 | **     notice, this list of conditions and the following disclaimer. | 
|---|
| 17 | **   * Redistributions in binary form must reproduce the above copyright | 
|---|
| 18 | **     notice, this list of conditions and the following disclaimer in | 
|---|
| 19 | **     the documentation and/or other materials provided with the | 
|---|
| 20 | **     distribution. | 
|---|
| 21 | **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor | 
|---|
| 22 | **     the names of its contributors may be used to endorse or promote | 
|---|
| 23 | **     products derived from this software without specific prior written | 
|---|
| 24 | **     permission. | 
|---|
| 25 | ** | 
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
|---|
| 37 | ** $QT_END_LICENSE$ | 
|---|
| 38 | ** | 
|---|
| 39 | ****************************************************************************/ | 
|---|
| 40 |  | 
|---|
| 41 | #ifndef DATABASE_H | 
|---|
| 42 | #define DATABASE_H | 
|---|
| 43 |  | 
|---|
| 44 | #include <QMessageBox> | 
|---|
| 45 | #include <QSqlDatabase> | 
|---|
| 46 | #include <QSqlError> | 
|---|
| 47 | #include <QSqlQuery> | 
|---|
| 48 |  | 
|---|
| 49 | static bool createConnection() | 
|---|
| 50 | { | 
|---|
| 51 | QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); | 
|---|
| 52 | db.setDatabaseName(":memory:"); | 
|---|
| 53 | if (!db.open()) { | 
|---|
| 54 | QMessageBox::critical(0, qApp->tr("Cannot open database"), | 
|---|
| 55 | qApp->tr("Unable to establish a database connection.\n" | 
|---|
| 56 | "This example needs SQLite support. Please read " | 
|---|
| 57 | "the Qt SQL driver documentation for information how " | 
|---|
| 58 | "to build it.\n\n" | 
|---|
| 59 | "Click Cancel to exit."), QMessageBox::Cancel); | 
|---|
| 60 | return false; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | QSqlQuery query; | 
|---|
| 64 |  | 
|---|
| 65 | query.exec("create table artists (id int primary key, " | 
|---|
| 66 | "artist varchar(40), " | 
|---|
| 67 | "albumcount int)"); | 
|---|
| 68 |  | 
|---|
| 69 | query.exec("insert into artists values(0, '<all>', 0)"); | 
|---|
| 70 | query.exec("insert into artists values(1, 'Ane Brun', 2)"); | 
|---|
| 71 | query.exec("insert into artists values(2, 'Thomas Dybdahl', 3)"); | 
|---|
| 72 | query.exec("insert into artists values(3, 'Kaizers Orchestra', 3)"); | 
|---|
| 73 |  | 
|---|
| 74 | query.exec("create table albums (albumid int primary key, " | 
|---|
| 75 | "title varchar(50), " | 
|---|
| 76 | "artistid int, " | 
|---|
| 77 | "year int)"); | 
|---|
| 78 |  | 
|---|
| 79 | query.exec("insert into albums values(1, 'Spending Time With Morgan', 1, " | 
|---|
| 80 | "2003)"); | 
|---|
| 81 | query.exec("insert into albums values(2, 'A Temporary Dive', 1, 2005)"); | 
|---|
| 82 | query.exec("insert into albums values(3, '...The Great October Sound', 2, " | 
|---|
| 83 | "2002)"); | 
|---|
| 84 | query.exec("insert into albums values(4, 'Stray Dogs', 2, 2003)"); | 
|---|
| 85 | query.exec("insert into albums values(5, " | 
|---|
| 86 | "'One day you`ll dance for me, New York City', 2, 2004)"); | 
|---|
| 87 | query.exec("insert into albums values(6, 'Ompa Til Du D\xf8r', 3, 2001)"); | 
|---|
| 88 | query.exec("insert into albums values(7, 'Evig Pint', 3, 2002)"); | 
|---|
| 89 | query.exec("insert into albums values(8, 'Maestro', 3, 2005)"); | 
|---|
| 90 |  | 
|---|
| 91 | return true; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | #endif | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|