Warning: That file was not part of the compilation database. It may have many parsing errors.

1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** You may use this file under the terms of the BSD license as follows:
10**
11** "Redistribution and use in source and binary forms, with or without
12** modification, are permitted provided that the following conditions are
13** met:
14** * Redistributions of source code must retain the above copyright
15** notice, this list of conditions and the following disclaimer.
16** * Redistributions in binary form must reproduce the above copyright
17** notice, this list of conditions and the following disclaimer in
18** the documentation and/or other materials provided with the
19** distribution.
20** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21** of its contributors may be used to endorse or promote products derived
22** from this software without specific prior written permission.
23**
24**
25** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36**
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]
58static 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
136

Warning: That file was not part of the compilation database. It may have many parsing errors.