| 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 CONNECTION_H | 
|---|
| 42 | #define CONNECTION_H | 
|---|
| 43 |  | 
|---|
| 44 | #include <QMessageBox> | 
|---|
| 45 | #include <QSqlDatabase> | 
|---|
| 46 | #include <QSqlError> | 
|---|
| 47 | #include <QSqlQuery> | 
|---|
| 48 |  | 
|---|
| 49 | /* | 
|---|
| 50 | This file defines a helper function to open a connection to an | 
|---|
| 51 | in-memory SQLITE database and to create a test table. | 
|---|
| 52 |  | 
|---|
| 53 | If you want to use another database, simply modify the code | 
|---|
| 54 | below. All the examples in this directory use this function to | 
|---|
| 55 | connect to a database. | 
|---|
| 56 | */ | 
|---|
| 57 | //! [0] | 
|---|
| 58 | static bool createConnection() | 
|---|
| 59 | { | 
|---|
| 60 | QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); | 
|---|
| 61 | db.setDatabaseName(":memory:"); | 
|---|
| 62 | if (!db.open()) { | 
|---|
| 63 | QMessageBox::critical(0, qApp->tr("Cannot open database"), | 
|---|
| 64 | qApp->tr("Unable to establish a database connection.\n" | 
|---|
| 65 | "This example needs SQLite support. Please read " | 
|---|
| 66 | "the Qt SQL driver documentation for information how " | 
|---|
| 67 | "to build it.\n\n" | 
|---|
| 68 | "Click Cancel to exit."), QMessageBox::Cancel); | 
|---|
| 69 | return false; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | QSqlQuery query; | 
|---|
| 73 | query.exec("create table person (id int primary key, " | 
|---|
| 74 | "firstname varchar(20), lastname varchar(20))"); | 
|---|
| 75 | query.exec("insert into person values(101, 'Danny', 'Young')"); | 
|---|
| 76 | query.exec("insert into person values(102, 'Christine', 'Holand')"); | 
|---|
| 77 | query.exec("insert into person values(103, 'Lars', 'Gordon')"); | 
|---|
| 78 | query.exec("insert into person values(104, 'Roberto', 'Robitaille')"); | 
|---|
| 79 | query.exec("insert into person values(105, 'Maria', 'Papadopoulos')"); | 
|---|
| 80 |  | 
|---|
| 81 | query.exec("create table offices (id int primary key," | 
|---|
| 82 | "imagefile int," | 
|---|
| 83 | "location varchar(20)," | 
|---|
| 84 | "country varchar(20)," | 
|---|
| 85 | "description varchar(100))"); | 
|---|
| 86 | query.exec("insert into offices " | 
|---|
| 87 | "values(0, 0, 'Oslo', 'Norway'," | 
|---|
| 88 | "'Oslo is home to more than 500 000 citizens and has a " | 
|---|
| 89 | "lot to offer.It has been called \"The city with the big " | 
|---|
| 90 | "heart\" and this is a nickname we are happy to live up to.')"); | 
|---|
| 91 | query.exec("insert into offices " | 
|---|
| 92 | "values(1, 1, 'Brisbane', 'Australia'," | 
|---|
| 93 | "'Brisbane is the capital of Queensland, the Sunshine State, " | 
|---|
| 94 | "where it is beautiful one day, perfect the next.  " | 
|---|
| 95 | "Brisbane is Australia''s 3rd largest city, being home " | 
|---|
| 96 | "to almost 2 million people.')"); | 
|---|
| 97 | query.exec("insert into offices " | 
|---|
| 98 | "values(2, 2, 'Redwood City', 'US'," | 
|---|
| 99 | "'You find Redwood City in the heart of the Bay Area " | 
|---|
| 100 | "just north of Silicon Valley. The largest nearby city is " | 
|---|
| 101 | "San Jose which is the third largest city in California " | 
|---|
| 102 | "and the 10th largest in the US.')"); | 
|---|
| 103 | query.exec("insert into offices " | 
|---|
| 104 | "values(3, 3, 'Berlin', 'Germany'," | 
|---|
| 105 | "'Berlin, the capital of Germany is dynamic, cosmopolitan " | 
|---|
| 106 | "and creative, allowing for every kind of lifestyle. " | 
|---|
| 107 | "East meets West in the metropolis at the heart of a " | 
|---|
| 108 | "changing Europe.')"); | 
|---|
| 109 | query.exec("insert into offices " | 
|---|
| 110 | "values(4, 4, 'Munich', 'Germany'," | 
|---|
| 111 | "'Several technology companies are represented in Munich, " | 
|---|
| 112 | "and the city is often called the \"Bavarian Silicon Valley\". " | 
|---|
| 113 | "The exciting city is also filled with culture, " | 
|---|
| 114 | "art and music. ')"); | 
|---|
| 115 | query.exec("insert into offices " | 
|---|
| 116 | "values(5, 5, 'Beijing', 'China'," | 
|---|
| 117 | "'Beijing as a capital city has more than 3000 years of " | 
|---|
| 118 | "history. Today the city counts 12 million citizens, and " | 
|---|
| 119 | "is the political, economic and cultural centre of China.')"); | 
|---|
| 120 |  | 
|---|
| 121 | query.exec("create table images (locationid int, file varchar(20))"); | 
|---|
| 122 | query.exec("insert into images values(0, 'images/oslo.png')"); | 
|---|
| 123 | query.exec("insert into images values(1, 'images/brisbane.png')"); | 
|---|
| 124 | query.exec("insert into images values(2, 'images/redwood.png')"); | 
|---|
| 125 | query.exec("insert into images values(3, 'images/berlin.png')"); | 
|---|
| 126 | query.exec("insert into images values(4, 'images/munich.png')"); | 
|---|
| 127 | query.exec("insert into images values(5, 'images/beijing.png')"); | 
|---|
| 128 |  | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 | return true; | 
|---|
| 132 | } | 
|---|
| 133 | //! [0] | 
|---|
| 134 |  | 
|---|
| 135 | #endif | 
|---|