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 DATABASE_H |
52 | #define DATABASE_H |
53 | |
54 | #include <QMessageBox> |
55 | #include <QSqlDatabase> |
56 | #include <QSqlError> |
57 | #include <QSqlQuery> |
58 | |
59 | static bool createConnection() |
60 | { |
61 | QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE" ); |
62 | db.setDatabaseName(":memory:" ); |
63 | if (!db.open()) { |
64 | QMessageBox::critical(nullptr, QObject::tr("Cannot open database" ), |
65 | QObject::tr("Unable to establish a database connection.\n" |
66 | "This example needs SQLite support. Please read " |
67 | "the Qt SQL driver documentation for information how " |
68 | "to build it.\n\n" |
69 | "Click Cancel to exit." ), QMessageBox::Cancel); |
70 | return false; |
71 | } |
72 | |
73 | QSqlQuery query; |
74 | |
75 | query.exec("create table artists (id int primary key, " |
76 | "artist varchar(40), " |
77 | "albumcount int)" ); |
78 | |
79 | query.exec("insert into artists values(0, '<all>', 0)" ); |
80 | query.exec("insert into artists values(1, 'Ane Brun', 2)" ); |
81 | query.exec("insert into artists values(2, 'Thomas Dybdahl', 3)" ); |
82 | query.exec("insert into artists values(3, 'Kaizers Orchestra', 3)" ); |
83 | |
84 | query.exec("create table albums (albumid int primary key, " |
85 | "title varchar(50), " |
86 | "artistid int, " |
87 | "year int)" ); |
88 | |
89 | query.exec("insert into albums values(1, 'Spending Time With Morgan', 1, " |
90 | "2003)" ); |
91 | query.exec("insert into albums values(2, 'A Temporary Dive', 1, 2005)" ); |
92 | query.exec("insert into albums values(3, '...The Great October Sound', 2, " |
93 | "2002)" ); |
94 | query.exec("insert into albums values(4, 'Stray Dogs', 2, 2003)" ); |
95 | query.exec("insert into albums values(5, " |
96 | "'One day you`ll dance for me, New York City', 2, 2004)" ); |
97 | query.exec("insert into albums values(6, 'Ompa Til Du D\xc3\xb8r', 3, 2001)" ); |
98 | query.exec("insert into albums values(7, 'Evig Pint', 3, 2002)" ); |
99 | query.exec("insert into albums values(8, 'Maestro', 3, 2005)" ); |
100 | |
101 | return true; |
102 | } |
103 | |
104 | #endif |
105 | |
106 | |
107 | |