Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
doc_src_sql-driver.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
#
include
<
QSqlDatabase
>
4
#
include
<
QSqlQuery
>
5
#
include
<
QSqlDriver
>
6
#
include
<
QSqlResult
>
7
#
include
<
QVariant
>
8
#
include
<
QDebug
>
9
10
void
testProc
()
11
{
12
//! [2]
13
QSqlQuery q;
14
q.exec(
"call qtestproc (@outval1, @outval2)"
);
15
q.exec(
"select @outval1, @outval2"
);
16
if
(q.next())
17
qDebug() << q.value(0) << q.value(1);
// outputs "42" and "43"
18
//! [2]
19
}
20
21
void
callStoredProc
()
22
{
23
//! [10]
24
// STORED_PROC uses the return statement or returns multiple result sets
25
QSqlQuery query;
26
query.setForwardOnly(
true
);
27
query.exec(
"{call STORED_PROC}"
);
28
//! [10]
29
}
30
31
void
setHost
()
32
{
33
//! [24]
34
QSqlDatabase db;
35
db.setHostName(
"MyServer"
);
36
db.setDatabaseName(
"C:\\test.gdb"
);
37
//! [24]
38
}
39
40
void
exProc
()
41
{
42
//! [26]
43
QSqlQuery q;
44
q.exec(
"execute procedure my_procedure"
);
45
if
(q.next())
46
qDebug() << q.value(0);
// outputs the first RETURN/OUT value
47
//! [26]
48
49
qDebug( \
50
"QSqlDatabase: QMYSQL driver not loaded \
51
QSqlDatabase: available drivers: QMYSQL"\
52
);
53
54
/* Commented because the following line is not compilable
55
//! [34]
56
column.contains(QRegularExpression("pattern"));
57
//! [34]
58
*/
59
}
60
61
62
63
void
updTable2
()
64
{
65
QSqlDatabase db;
66
//! [37]
67
int
value;
68
QSqlQuery query1;
69
query1.setForwardOnly(
true
);
70
query1.exec(
"select * FROM table1"
);
71
while
(query1.next()) {
72
value = query1.value(0).toInt();
73
if
(value == 1) {
74
QSqlQuery query2;
75
query2.exec(
"update table2 set col=2"
);
// WRONG: This will discard all results of
76
}
// query1, and cause the loop to quit
77
}
78
//! [37]
79
}
80
81
void
callOutProc
()
82
{
83
//! [40]
84
QSqlDatabase db;
85
QSqlQuery query;
86
int
i1 = 10, i2 = 0;
87
query.prepare(
"call qtestproc(?, ?)"
);
88
query.bindValue(0, i1, QSql::InOut);
89
query.bindValue(1, i2, QSql::Out);
90
query.exec();
91
//! [40]
92
}
exProc
void exProc()
Definition
doc_src_sql-driver.cpp:40
callOutProc
void callOutProc()
Definition
doc_src_sql-driver.cpp:81
setHost
void setHost()
Definition
doc_src_sql-driver.cpp:31
testProc
void testProc()
Definition
doc_src_sql-driver.cpp:10
callStoredProc
void callStoredProc()
Definition
doc_src_sql-driver.cpp:21
updTable2
void updTable2()
Definition
doc_src_sql-driver.cpp:63
qtbase
src
sql
doc
snippets
code
doc_src_sql-driver.cpp
Generated on
for Qt by
1.14.0