Qt 5.10 Book Examples
chapter41
SQLQueryModel
main.cpp
Go to the documentation of this file.
1
// ======================================================================
2
// main.cpp
3
// ======================================================================
4
// This file is a part of the book
5
// "Qt 5.10 Professional programming with C++"
6
// http://qt-book.com
7
// ======================================================================
8
// Copyright (c) 2017 by Max Schlee
9
// ======================================================================
10
11
#include <QtWidgets>
12
#include <QtSql>
13
14
static
bool
createConnection()
15
{
16
QSqlDatabase db = QSqlDatabase::addDatabase(
"QSQLITE"
);
17
db.setDatabaseName(
"addressbook"
);
18
19
db.setUserName(
"elton"
);
20
db.setHostName(
"epica"
);
21
db.setPassword(
"password"
);
22
if
(!db.open()) {
23
qDebug() <<
"Cannot open database:"
<< db.lastError();
24
return
false
;
25
}
26
return
true
;
27
}
28
29
// ----------------------------------------------------------------------
30
int
main
(
int
argc,
char
** argv)
31
{
32
QApplication app(argc, argv);
33
34
if
(!createConnection()) {
35
return
-1;
36
}
37
38
QTableView view;
39
QSqlQueryModel model;
40
model.setQuery(
"SELECT phone, email "
41
"FROM addressbook "
42
"WHERE name = 'Piggy';"
43
);
44
45
if
(model.lastError().isValid()) {
46
qDebug() << model.lastError();
47
}
48
49
view.setModel(&model);
50
view.show();
51
52
return
app.exec();
53
}
54
55
main
int main(int argc, char **argv)
Definition:
main.cpp:15
Generated by
1.8.14