Qt 6.x
The Qt SDK
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
11{
14q.exec("call qtestproc (@outval1, @outval2)");
15q.exec("select @outval1, @outval2");
16if (q.next())
17 qDebug() << q.value(0) << q.value(1); // outputs "42" and "43"
19}
20
22{
24// STORED_PROC uses the return statement or returns multiple result sets
26query.setForwardOnly(true);
27query.exec("{call STORED_PROC}");
29}
30
31void setHost()
32{
35db.setHostName("MyServer");
36db.setDatabaseName("C:\\test.gdb");
38}
39
40void exProc()
41{
44q.exec("execute procedure my_procedure");
45if (q.next())
46 qDebug() << q.value(0); // outputs the first RETURN/OUT value
48
49qDebug( \
50"QSqlDatabase: QMYSQL driver not loaded \
51QSqlDatabase: available drivers: QMYSQL" \
52);
53
54/* Commented because the following line is not compilable
56column.contains(QRegularExpression("pattern"));
58*/
59}
60
61
62
64{
67int value;
68QSqlQuery query1;
69query1.setForwardOnly(true);
70query1.exec("select * FROM table1");
71while (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}
79}
80
82{
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();
92}
The QSqlDatabase class handles a connection to a database.
The QSqlQuery class provides a means of executing and manipulating SQL statements.
Definition qsqlquery.h:23
bool next()
Retrieves the next record in the result, if available, and positions the query on the retrieved recor...
QVariant value(int i) const
Returns the value of field index in the current record.
void setForwardOnly(bool forward)
Sets forward only mode to forward.
bool exec(const QString &query)
Executes the SQL in query.
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
void exProc()
void callOutProc()
void setHost()
void testProc()
void callStoredProc()
void updTable2()
@ InOut
Definition qtsqlglobal.h:30
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qDebug
[1]
Definition qlogging.h:160
GLenum query
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
QMimeDatabase db
[0]