Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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{
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
22q.prepare("insert into myTable values (?, ?)");
23
25ints << 1 << 2 << 3 << 4;
26q.addBindValue(ints);
27
29names << "Harald" << "Boris" << "Trond" << QVariant(QMetaType::fromType<QString>());
30q.addBindValue(names);
31
32if (!q.execBatch())
33 qDebug() << q.lastError();
35}
The QSqlQuery class provides a means of executing and manipulating SQL statements.
Definition qsqlquery.h:23
The QSqlRecord class encapsulates a database record.
Definition qsqlrecord.h:20
int count() const
Returns the number of fields in the record.
int indexOf(const QString &name) const
Returns the position of the field called name within the record, or -1 if it cannot be found.
\inmodule QtCore
Definition qvariant.h:64
EGLint EGLint EGLint EGLint int int * ints
#define qDebug
[1]
Definition qlogging.h:160
GLuint GLuint * names
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
void selectEmployees()