1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#ifndef CONNECTION_H
52#define CONNECTION_H
53
54#include <QMessageBox>
55#include <QSqlDatabase>
56#include <QSqlError>
57#include <QSqlQuery>
58
59/*
60 This file defines a helper function to open a connection to an
61 in-memory SQLITE database and to create a test table.
62
63 If you want to use another database, simply modify the code
64 below. All the examples in this directory use this function to
65 connect to a database.
66*/
67//! [0]
68static bool createConnection()
69{
70 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
71 db.setDatabaseName(":memory:");
72 if (!db.open()) {
73 QMessageBox::critical(nullptr, QObject::tr("Cannot open database"),
74 QObject::tr("Unable to establish a database connection.\n"
75 "This example needs SQLite support. Please read "
76 "the Qt SQL driver documentation for information how "
77 "to build it.\n\n"
78 "Click Cancel to exit."), QMessageBox::Cancel);
79 return false;
80 }
81
82 QSqlQuery query;
83 query.exec("create table person (id int primary key, "
84 "firstname varchar(20), lastname varchar(20))");
85 query.exec("insert into person values(101, 'Danny', 'Young')");
86 query.exec("insert into person values(102, 'Christine', 'Holand')");
87 query.exec("insert into person values(103, 'Lars', 'Gordon')");
88 query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
89 query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");
90
91 query.exec("create table items (id int primary key,"
92 "imagefile int,"
93 "itemtype varchar(20),"
94 "description varchar(100))");
95 query.exec("insert into items "
96 "values(0, 0, 'Qt',"
97 "'Qt is a full development framework with tools designed to "
98 "streamline the creation of stunning applications and "
99 "amazing user interfaces for desktop, embedded and mobile "
100 "platforms.')");
101 query.exec("insert into items "
102 "values(1, 1, 'Qt Quick',"
103 "'Qt Quick is a collection of techniques designed to help "
104 "developers create intuitive, modern-looking, and fluid "
105 "user interfaces using a CSS & JavaScript like language.')");
106 query.exec("insert into items "
107 "values(2, 2, 'Qt Creator',"
108 "'Qt Creator is a powerful cross-platform integrated "
109 "development environment (IDE), including UI design tools "
110 "and on-device debugging.')");
111 query.exec("insert into items "
112 "values(3, 3, 'Qt Project',"
113 "'The Qt Project governs the open source development of Qt, "
114 "allowing anyone wanting to contribute to join the effort "
115 "through a meritocratic structure of approvers and "
116 "maintainers.')");
117
118 query.exec("create table images (itemid int, file varchar(20))");
119 query.exec("insert into images values(0, 'images/qt-logo.png')");
120 query.exec("insert into images values(1, 'images/qt-quick.png')");
121 query.exec("insert into images values(2, 'images/qt-creator.png')");
122 query.exec("insert into images values(3, 'images/qt-project.png')");
123
124 return true;
125}
126//! [0]
127
128#endif
129