1#############################################################################
2##
3## Copyright (C) 2013 Riverbank Computing Limited.
4## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5## All rights reserved.
6##
7## This file is part of the examples of PyQt.
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
42from PyQt5.QtWidgets import QMessageBox
43from PyQt5.QtSql import QSqlDatabase, QSqlQuery
44
45
46def createConnection():
47    db = QSqlDatabase.addDatabase('QSQLITE')
48    db.setDatabaseName(':memory:')
49    if not db.open():
50        QMessageBox.critical(None, "Cannot open database",
51                "Unable to establish a database connection.\n"
52                "This example needs SQLite support. Please read the Qt SQL "
53                "driver documentation for information how to build it.\n\n"
54                "Click Cancel to exit.",
55                QMessageBox.Cancel)
56        return False
57
58    query = QSqlQuery()
59    query.exec_("create table person(id int primary key, "
60                "firstname varchar(20), lastname varchar(20))")
61    query.exec_("insert into person values(101, 'Danny', 'Young')")
62    query.exec_("insert into person values(102, 'Christine', 'Holand')")
63    query.exec_("insert into person values(103, 'Lars', 'Gordon')")
64    query.exec_("insert into person values(104, 'Roberto', 'Robitaille')")
65    query.exec_("insert into person values(105, 'Maria', 'Papadopoulos')")
66
67    query.exec_("create table offices (id int primary key,"
68                                             "imagefile int,"
69                                             "location varchar(20),"
70                                             "country varchar(20),"
71                                             "description varchar(100))");
72    query.exec_("insert into offices "
73               "values(0, 0, 'Oslo', 'Norway',"
74               "'Oslo is home to more than 500 000 citizens and has a "
75               "lot to offer.It has been called \"The city with the big "
76               "heart\" and this is a nickname we are happy to live up to.')")
77    query.exec_("insert into offices "
78               "values(1, 1, 'Brisbane', 'Australia',"
79               "'Brisbane is the capital of Queensland, the Sunshine State, "
80               "where it is beautiful one day, perfect the next.  "
81               "Brisbane is Australia''s 3rd largest city, being home "
82               "to almost 2 million people.')")
83    query.exec_("insert into offices "
84               "values(2, 2, 'Redwood City', 'US',"
85               "'You find Redwood City in the heart of the Bay Area "
86               "just north of Silicon Valley. The largest nearby city is "
87               "San Jose which is the third largest city in California "
88               "and the 10th largest in the US.')")
89    query.exec_("insert into offices "
90               "values(3, 3, 'Berlin', 'Germany',"
91               "'Berlin, the capital of Germany is dynamic, cosmopolitan "
92               "and creative, allowing for every kind of lifestyle. "
93               "East meets West in the metropolis at the heart of a "
94               "changing Europe.')")
95    query.exec_("insert into offices "
96               "values(4, 4, 'Munich', 'Germany',"
97               "'Several technology companies are represented in Munich, "
98               "and the city is often called the \"Bavarian Silicon Valley\". "
99               "The exciting city is also filled with culture, "
100               "art and music. ')")
101    query.exec_("insert into offices "
102               "values(5, 5, 'Beijing', 'China',"
103               "'Beijing as a capital city has more than 3000 years of "
104               "history. Today the city counts 12 million citizens, and "
105               "is the political, economic and cultural centre of China.')")
106
107    query.exec_("create table images (locationid int, file varchar(20))")
108    query.exec_("insert into images values(0, 'images/oslo.png')")
109    query.exec_("insert into images values(1, 'images/brisbane.png')")
110    query.exec_("insert into images values(2, 'images/redwood.png')")
111    query.exec_("insert into images values(3, 'images/berlin.png')")
112    query.exec_("insert into images values(4, 'images/munich.png')")
113    query.exec_("insert into images values(5, 'images/beijing.png')")
114
115    return True
116