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
src_sql_kernel_qsqlquery.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 <QDebug>
7
9{
10//! [1]
11QSqlQuery q("select * from employees");
12QSqlRecord rec = q.record();
13
14qDebug() << "Number of columns: " << rec.count();
15
16int nameCol = rec.indexOf("name"); // index of the field "name"
17while (q.next())
18 qDebug() << q.value(nameCol).toString(); // output all names
19//! [1]
20//! [2]
21QSqlQuery q;
22q.prepare("insert into myTable values (?, ?)");
23
24QVariantList ints;
25ints << 1 << 2 << 3 << 4;
26q.addBindValue(ints);
27
28QVariantList names;
29names << "Harald" << "Boris" << "Trond" << QVariant(QMetaType::fromType<QString>());
30q.addBindValue(names);
31
32if (!q.execBatch())
33 qDebug() << q.lastError();
34//! [2]
35}
void selectEmployees()