| 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 |
| 11 | typedef void *sqlite3_stmt; |
| 12 | |
| 13 | void insertVariants() |
| 14 | { |
| 15 | //! [0] |
| 16 | QSqlQuery q; |
| 17 | q.prepare(query: "insert into test (i1, i2, s) values (?, ?, ?)" ); |
| 18 | |
| 19 | QVariantList col1; |
| 20 | QVariantList col2; |
| 21 | QVariantList col3; |
| 22 | |
| 23 | col1 << 1 << 3; |
| 24 | col2 << 2 << 4; |
| 25 | col3 << "hello" << "world" ; |
| 26 | |
| 27 | q.bindValue(pos: 0, val: col1); |
| 28 | q.bindValue(pos: 1, val: col2); |
| 29 | q.bindValue(pos: 2, val: col3); |
| 30 | |
| 31 | if (!q.execBatch()) |
| 32 | qDebug() << q.lastError(); |
| 33 | //! [0] |
| 34 | } |
| 35 | |
| 36 | void querySqlite() |
| 37 | { |
| 38 | //! [1] |
| 39 | QSqlDatabase db = QSqlDatabase::database(connectionName: "sales" ); |
| 40 | QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES" , db); |
| 41 | |
| 42 | QVariant v = query.result()->handle(); |
| 43 | if (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 | |