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 <QSqlError>
7#include <QSqlResult>
8#include <QDebug>
9
10// dummy typedef
11typedef void *sqlite3_stmt;
12
13void insertVariants()
14{
15//! [0]
16QSqlQuery q;
17q.prepare(query: "insert into test (i1, i2, s) values (?, ?, ?)");
18
19QVariantList col1;
20QVariantList col2;
21QVariantList col3;
22
23col1 << 1 << 3;
24col2 << 2 << 4;
25col3 << "hello" << "world";
26
27q.bindValue(pos: 0, val: col1);
28q.bindValue(pos: 1, val: col2);
29q.bindValue(pos: 2, val: col3);
30
31if (!q.execBatch())
32 qDebug() << q.lastError();
33//! [0]
34}
35
36void querySqlite()
37{
38//! [1]
39QSqlDatabase db = QSqlDatabase::database(connectionName: "sales");
40QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
41
42QVariant v = query.result()->handle();
43if (v.isValid() && qstrcmp(str1: v.typeName(), str2: "sqlite3_stmt*") == 0) {
44 // v.data() returns a pointer to the handle
45 sqlite3_stmt *handle = *static_cast<sqlite3_stmt **>(v.data());
46 if (handle) {
47 // ...
48 }
49}
50//! [1]
51}
52

source code of qtbase/src/sql/doc/snippets/code/src_sql_kernel_qsqlresult.cpp