Doc: make Qt Sql snippets compilable
Task-number: QTBUG-81496 Change-Id: Id6206e9179c2e8157c99e777a3de35bd83d49e34 Reviewed-by: Topi Reiniö <topi.reinio@qt.io> Reviewed-by: Paul Wicking <paul.wicking@qt.io>bb10
parent
b77e239c5e
commit
0359a82e6e
|
|
@ -47,7 +47,15 @@
|
|||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlDriver>
|
||||
#include <QSqlResult>
|
||||
#include <QVariant>
|
||||
#include <QDebug>
|
||||
|
||||
void testProc()
|
||||
{
|
||||
//! [2]
|
||||
QSqlQuery q;
|
||||
q.exec("call qtestproc (@outval1, @outval2)");
|
||||
|
|
@ -55,17 +63,22 @@ q.exec("select @outval1, @outval2");
|
|||
q.next();
|
||||
qDebug() << q.value(0) << q.value(1); // outputs "42" and "43"
|
||||
//! [2]
|
||||
}
|
||||
|
||||
|
||||
void callStoredProc()
|
||||
{
|
||||
//! [10]
|
||||
// STORED_PROC uses the return statement or returns multiple result sets
|
||||
QSqlQuery query;
|
||||
query.setForwardOnly(true);
|
||||
query.exec("{call STORED_PROC}");
|
||||
//! [10]
|
||||
}
|
||||
|
||||
|
||||
void setHost()
|
||||
{
|
||||
//! [24]
|
||||
QSqlDatabase db;
|
||||
db.setHostName("MyServer");
|
||||
db.setDatabaseName("C:\\test.gdb");
|
||||
//! [24]
|
||||
|
|
@ -76,8 +89,10 @@ db.setDatabaseName("C:\\test.gdb");
|
|||
db.setConnectOptions("ISC_DPB_LC_CTYPE=Latin1");
|
||||
db.open();
|
||||
//! [25]
|
||||
}
|
||||
|
||||
|
||||
void exProc()
|
||||
{
|
||||
//! [26]
|
||||
QSqlQuery q;
|
||||
q.exec("execute procedure my_procedure");
|
||||
|
|
@ -85,50 +100,43 @@ q.next();
|
|||
qDebug() << q.value(0); // outputs the first RETURN/OUT value
|
||||
//! [26]
|
||||
|
||||
|
||||
qDebug( \
|
||||
//! [31]
|
||||
QSqlDatabase: QMYSQL driver not loaded
|
||||
QSqlDatabase: available drivers: QMYSQL
|
||||
"QSqlDatabase: QMYSQL driver not loaded \
|
||||
QSqlDatabase: available drivers: QMYSQL" \
|
||||
//! [31]
|
||||
);
|
||||
|
||||
|
||||
/* Commented because the following line is not compilable
|
||||
//! [34]
|
||||
column.contains(QRegularExpression("pattern"));
|
||||
//! [34]
|
||||
|
||||
|
||||
//! [36]
|
||||
QSqlQuery query(db);
|
||||
query.setForwardOnly(true);
|
||||
query.exec("SELECT * FROM table");
|
||||
while (query.next()) {
|
||||
// Handle changes in every iteration of the loop
|
||||
QVariant v = query.result()->handle();
|
||||
if (qstrcmp(v.typeName(), "PGresult*") == 0) {
|
||||
PGresult *handle = *static_cast<PGresult **>(v.data());
|
||||
if (handle) {
|
||||
// Do something...
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
//! [36]
|
||||
|
||||
|
||||
|
||||
void updTable2()
|
||||
{
|
||||
QSqlDatabase db;
|
||||
//! [37]
|
||||
int value;
|
||||
QSqlQuery query1(db);
|
||||
QSqlQuery query1;
|
||||
query1.setForwardOnly(true);
|
||||
query1.exec("select * FROM table1");
|
||||
while (query1.next()) {
|
||||
value = query1.value(0).toInt();
|
||||
if (value == 1) {
|
||||
QSqlQuery query2(db);
|
||||
QSqlQuery query2;
|
||||
query2.exec("update table2 set col=2"); // WRONG: This will discard all results of
|
||||
} // query1, and cause the loop to quit
|
||||
}
|
||||
//! [37]
|
||||
}
|
||||
|
||||
|
||||
void setConnString()
|
||||
{
|
||||
//! [39]
|
||||
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC3");
|
||||
QString connectString = QStringLiteral(
|
||||
|
|
@ -139,3 +147,4 @@ QString connectString = QStringLiteral(
|
|||
"SCROLLABLERESULT=true");
|
||||
db.setDatabaseName(connectString);
|
||||
//! [39]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,66 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2020 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the documentation of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** BSD License Usage
|
||||
** Alternatively, you may use this file under the terms of the BSD license
|
||||
** as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of The Qt Company Ltd nor the names of its
|
||||
** contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
//! [36]
|
||||
QSqlQuery query;
|
||||
QVariant v;
|
||||
query.setForwardOnly(true);
|
||||
query.exec("SELECT * FROM table");
|
||||
while (query.next()) {
|
||||
// Handle changes in every iteration of the loop
|
||||
v = query.result()->handle();
|
||||
|
||||
if (qstrcmp(v.typeName(), "PGresult*") == 0) {
|
||||
PGresult *handle = *static_cast<PGresult **>(v.data());
|
||||
if (handle) {
|
||||
// Do something...
|
||||
}
|
||||
}
|
||||
}
|
||||
//! [36]
|
||||
|
|
@ -47,18 +47,25 @@
|
|||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlDriver>
|
||||
#include <QDebug>
|
||||
|
||||
void openDatabase()
|
||||
{
|
||||
//! [0]
|
||||
// WRONG
|
||||
QSqlDatabase db = QSqlDatabase::database("sales");
|
||||
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
|
||||
QSqlDatabase::removeDatabase("sales"); // will output a warning
|
||||
|
||||
// "db" is now a dangling invalid database connection,
|
||||
// "query" contains an invalid result set
|
||||
//! [0]
|
||||
}
|
||||
|
||||
|
||||
void removeDatabase()
|
||||
{
|
||||
//! [1]
|
||||
{
|
||||
QSqlDatabase db = QSqlDatabase::database("sales");
|
||||
|
|
@ -67,72 +74,51 @@ QSqlDatabase::removeDatabase("sales"); // will output a warning
|
|||
// Both "db" and "query" are destroyed because they are out of scope
|
||||
QSqlDatabase::removeDatabase("sales"); // correct
|
||||
//! [1]
|
||||
}
|
||||
|
||||
|
||||
//! [2]
|
||||
QSqlDatabase::registerSqlDriver("MYDRIVER",
|
||||
new QSqlDriverCreator<MyDatabaseDriver>);
|
||||
QSqlDatabase db = QSqlDatabase::addDatabase("MYDRIVER");
|
||||
//! [2]
|
||||
|
||||
|
||||
void setmyDatabase()
|
||||
{
|
||||
//! [3]
|
||||
...
|
||||
db = QSqlDatabase::addDatabase("QODBC");
|
||||
// ...
|
||||
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
|
||||
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=myaccessfile.mdb");
|
||||
if (db.open()) {
|
||||
// success!
|
||||
}
|
||||
...
|
||||
// ...
|
||||
//! [3]
|
||||
}
|
||||
|
||||
|
||||
//! [4]
|
||||
...
|
||||
// ...
|
||||
// MySQL connection
|
||||
void dbConnect()
|
||||
{
|
||||
QSqlDatabase db;
|
||||
//! [4]
|
||||
db.setConnectOptions("SSL_KEY=client-key.pem;SSL_CERT=client-cert.pem;SSL_CA=ca-cert.pem;CLIENT_IGNORE_SPACE=1"); // use an SSL connection to the server
|
||||
if (!db.open()) {
|
||||
db.setConnectOptions(); // clears the connect option string
|
||||
...
|
||||
// ...
|
||||
}
|
||||
...
|
||||
// ...
|
||||
// PostgreSQL connection
|
||||
db.setConnectOptions("requiressl=1"); // enable PostgreSQL SSL connections
|
||||
if (!db.open()) {
|
||||
db.setConnectOptions(); // clear options
|
||||
...
|
||||
// ...
|
||||
}
|
||||
...
|
||||
// ...
|
||||
// ODBC connection
|
||||
db.setConnectOptions("SQL_ATTR_ACCESS_MODE=SQL_MODE_READ_ONLY;SQL_ATTR_TRACE=SQL_OPT_TRACE_ON"); // set ODBC options
|
||||
if (!db.open()) {
|
||||
db.setConnectOptions(); // don't try to set this option
|
||||
...
|
||||
// ...
|
||||
}
|
||||
}
|
||||
//! [4]
|
||||
|
||||
|
||||
//! [5]
|
||||
#include "qtdir/src/sql/drivers/psql/qsql_psql.cpp"
|
||||
//! [5]
|
||||
|
||||
|
||||
//! [6]
|
||||
PGconn *con = PQconnectdb("host=server user=bart password=simpson dbname=springfield");
|
||||
QPSQLDriver *drv = new QPSQLDriver(con);
|
||||
QSqlDatabase db = QSqlDatabase::addDatabase(drv); // becomes the new default connection
|
||||
QSqlQuery query;
|
||||
query.exec("SELECT NAME, ID FROM STAFF");
|
||||
...
|
||||
//! [6]
|
||||
|
||||
|
||||
//! [7]
|
||||
unix:LIBS += -lpq
|
||||
win32:LIBS += libpqdll.lib
|
||||
//! [7]
|
||||
|
||||
|
||||
void dbQdebug()
|
||||
{
|
||||
//! [8]
|
||||
QSqlDatabase db;
|
||||
qDebug() << db.isValid(); // Returns false
|
||||
|
|
@ -143,3 +129,4 @@ qDebug() << db.isValid(); // Returns \c true if "sales" connection exists
|
|||
QSqlDatabase::removeDatabase("sales");
|
||||
qDebug() << db.isValid(); // Returns false
|
||||
//! [8]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,69 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2020 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the documentation of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** BSD License Usage
|
||||
** Alternatively, you may use this file under the terms of the BSD license
|
||||
** as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of The Qt Company Ltd nor the names of its
|
||||
** contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
//! [2]
|
||||
QSqlDatabase::registerSqlDriver("MYDRIVER", new QSqlDriverCreator<QSqlDriver>);
|
||||
QVERIFY(QSqlDatabase::drivers().contains("MYDRIVER"));
|
||||
QSqlDatabase db = QSqlDatabase::addDatabase("MYDRIVER");
|
||||
QVERIFY(db.isValid());
|
||||
//! [2]
|
||||
//! [6]
|
||||
PGconn *con = PQconnectdb("host=server user=bart password=simpson dbname=springfield");
|
||||
QPSQLDriver *drv = new QPSQLDriver(con);
|
||||
QSqlDatabase db = QSqlDatabase::addDatabase(drv); // becomes the new default connection
|
||||
QSqlQuery query;
|
||||
query.exec("SELECT NAME, ID FROM STAFF");
|
||||
//! [6]
|
||||
|
||||
//! [7]
|
||||
unix:LIBS += -lpq
|
||||
win32:LIBS += libpqdll.lib
|
||||
//! [7]
|
||||
|
||||
|
|
@ -47,28 +47,42 @@
|
|||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlDriver>
|
||||
#include <QVariant>
|
||||
|
||||
void checkHandle()
|
||||
{
|
||||
//dummy definitions
|
||||
typedef void sqlite3;
|
||||
typedef void PGconn;
|
||||
typedef void MYSQL;
|
||||
//! [0]
|
||||
QSqlDatabase db = ...;
|
||||
QSqlDatabase db = QSqlDatabase::database();
|
||||
QVariant v = db.driver()->handle();
|
||||
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*") == 0) {
|
||||
if (v.isValid() && (qstrcmp(v.typeName(), "sqlite3*") == 0)) {
|
||||
// v.data() returns a pointer to the handle
|
||||
sqlite3 *handle = *static_cast<sqlite3 **>(v.data());
|
||||
if (handle) {
|
||||
...
|
||||
// ...
|
||||
}
|
||||
}
|
||||
//! [0]
|
||||
|
||||
|
||||
//! [1]
|
||||
if (qstrcmp(v.typeName(), "PGconn*") == 0) {
|
||||
PGconn *handle = *static_cast<PGconn **>(v.data());
|
||||
if (handle) ...
|
||||
if (handle) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
if (qstrcmp(v.typeName(), "MYSQL*") == 0) {
|
||||
MYSQL *handle = *static_cast<MYSQL **>(v.data());
|
||||
if (handle) ...
|
||||
if (handle) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
//! [1]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,10 +47,18 @@
|
|||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlQueryModel>
|
||||
#include <QSqlError>
|
||||
#include <QDebug>
|
||||
|
||||
void checkSqlQueryModel()
|
||||
{
|
||||
//! [0]
|
||||
QSqlQueryModel model;
|
||||
model.setQuery("select * from myTable");
|
||||
if (model.lastError().isValid())
|
||||
qDebug() << model.lastError();
|
||||
//! [0]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,12 +47,13 @@
|
|||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlDriver>
|
||||
#include <QDebug>
|
||||
|
||||
//! [0]
|
||||
SELECT forename, surname FROM people;
|
||||
//! [0]
|
||||
|
||||
|
||||
void selectEmployees()
|
||||
{
|
||||
//! [1]
|
||||
QSqlQuery q("select * from employees");
|
||||
QSqlRecord rec = q.record();
|
||||
|
|
@ -63,8 +64,6 @@ int nameCol = rec.indexOf("name"); // index of the field "name"
|
|||
while (q.next())
|
||||
qDebug() << q.value(nameCol).toString(); // output all names
|
||||
//! [1]
|
||||
|
||||
|
||||
//! [2]
|
||||
QSqlQuery q;
|
||||
q.prepare("insert into myTable values (?, ?)");
|
||||
|
|
@ -80,11 +79,4 @@ q.addBindValue(names);
|
|||
if (!q.execBatch())
|
||||
qDebug() << q.lastError();
|
||||
//! [2]
|
||||
|
||||
|
||||
//! [3]
|
||||
1 Harald
|
||||
2 Boris
|
||||
3 Trond
|
||||
4 NULL
|
||||
//! [3]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2020 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the documentation of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** BSD License Usage
|
||||
** Alternatively, you may use this file under the terms of the BSD license
|
||||
** as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of The Qt Company Ltd nor the names of its
|
||||
** contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
//! [0]
|
||||
SELECT forename, surname FROM people;
|
||||
//! [0]
|
||||
//! [3]
|
||||
1 Harald
|
||||
2 Boris
|
||||
3 Trond
|
||||
4 NULL
|
||||
//! [3]
|
||||
|
|
@ -47,7 +47,18 @@
|
|||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlDriver>
|
||||
#include <QSqlError>
|
||||
#include <QSqlResult>
|
||||
#include <QDebug>
|
||||
|
||||
// dummy typedef
|
||||
typedef void *sqlite3_stmt;
|
||||
|
||||
void insertVariants()
|
||||
{
|
||||
//! [0]
|
||||
QSqlQuery q;
|
||||
q.prepare("insert into test (i1, i2, s) values (?, ?, ?)");
|
||||
|
|
@ -67,29 +78,21 @@ q.bindValue(2, col3);
|
|||
if (!q.execBatch())
|
||||
qDebug() << q.lastError();
|
||||
//! [0]
|
||||
}
|
||||
|
||||
|
||||
void querySqlite()
|
||||
{
|
||||
//! [1]
|
||||
QSqlQuery query = ...
|
||||
QSqlDatabase db = QSqlDatabase::database("sales");
|
||||
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
|
||||
|
||||
QVariant v = query.result()->handle();
|
||||
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3_stmt*") == 0) {
|
||||
// v.data() returns a pointer to the handle
|
||||
sqlite3_stmt *handle = *static_cast<sqlite3_stmt **>(v.data());
|
||||
if (handle) {
|
||||
...
|
||||
// ...
|
||||
}
|
||||
}
|
||||
//! [1]
|
||||
|
||||
|
||||
//! [2]
|
||||
if (qstrcmp(v.typeName(), "PGresult*") == 0) {
|
||||
PGresult *handle = *static_cast<PGresult **>(v.data());
|
||||
if (handle) ...
|
||||
}
|
||||
|
||||
if (qstrcmp(v.typeName(), "MYSQL_STMT*") == 0) {
|
||||
MYSQL_STMT *handle = *static_cast<MYSQL_STMT **>(v.data());
|
||||
if (handle) ...
|
||||
}
|
||||
//! [2]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2020 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the documentation of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** BSD License Usage
|
||||
** Alternatively, you may use this file under the terms of the BSD license
|
||||
** as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of The Qt Company Ltd nor the names of its
|
||||
** contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
//! [2]
|
||||
if (qstrcmp(v.typeName(), "PGresult*") == 0) {
|
||||
PGresult *handle = *static_cast<PGresult **>(v.data());
|
||||
if (handle) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
if (qstrcmp(v.typeName(), "MYSQL_STMT*") == 0) {
|
||||
MYSQL_STMT *handle = *static_cast<MYSQL_STMT **>(v.data());
|
||||
if (handle) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
//! [2]
|
||||
|
|
@ -47,16 +47,24 @@
|
|||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlDriver>
|
||||
#include <QVariant>
|
||||
#include "../sqldatabase/sqldatabase.cpp"
|
||||
|
||||
void MyModel::fetchModel()
|
||||
{
|
||||
MyModel *myModel = new MyModel;
|
||||
//! [0]
|
||||
while (myModel->canFetchMore())
|
||||
myModel->fetchMore();
|
||||
//! [0]
|
||||
|
||||
|
||||
//! [1]
|
||||
QSqlQueryModel model;
|
||||
model.setQuery("select * from MyTable");
|
||||
if (model.lastError().isValid())
|
||||
qDebug() << model.lastError();
|
||||
//! [1]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
TEMPLATE = app
|
||||
TARGET = sqldatabase_cppsnippet
|
||||
QT = core sql sql-private
|
||||
|
||||
SOURCES += sqldatabase/sqldatabase.cpp \
|
||||
code/doc_src_qtsql.cpp \
|
||||
code/doc_src_sql-driver.cpp \
|
||||
code/src_sql_kernel_qsqldatabase.cpp \
|
||||
code/src_sql_kernel_qsqlerror.cpp \
|
||||
code/src_sql_kernel_qsqlresult.cpp \
|
||||
code/src_sql_kernel_qsqldriver.cpp \
|
||||
code/src_sql_models_qsqlquerymodel.cpp
|
||||
|
||||
|
|
@ -48,18 +48,13 @@
|
|||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtGui>
|
||||
#include <QCoreApplication>
|
||||
#include <QtSql>
|
||||
|
||||
#include <QMap>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
QString tr(const char *text)
|
||||
{
|
||||
return QApplication::translate(text, text);
|
||||
}
|
||||
|
||||
void QSqlDatabase_snippets()
|
||||
{
|
||||
{
|
||||
|
|
@ -209,9 +204,8 @@ void QSqlQuery_snippets()
|
|||
{
|
||||
// examine with named binding
|
||||
//! [14]
|
||||
QMapIterator<QString, QVariant> i(query.boundValues());
|
||||
while (i.hasNext()) {
|
||||
i.next();
|
||||
QMap<QString, QVariant> sqlIterator(query.boundValues());
|
||||
for (auto i = sqlIterator.begin(); i != sqlIterator.end(); ++i) {
|
||||
cout << i.key().toUtf8().data() << ": "
|
||||
<< i.value().toString().toUtf8().data() << Qt::endl;
|
||||
}
|
||||
|
|
@ -230,23 +224,6 @@ void QSqlQuery_snippets()
|
|||
|
||||
void QSqlQueryModel_snippets()
|
||||
{
|
||||
{
|
||||
//! [16]
|
||||
QSqlQueryModel *model = new QSqlQueryModel;
|
||||
model->setQuery("SELECT name, salary FROM employee");
|
||||
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
|
||||
model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
|
||||
|
||||
//! [17]
|
||||
QTableView *view = new QTableView;
|
||||
//! [17] //! [18]
|
||||
view->setModel(model);
|
||||
//! [18] //! [19]
|
||||
view->show();
|
||||
//! [16] //! [19] //! [20]
|
||||
view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
//! [20]
|
||||
}
|
||||
|
||||
//! [21]
|
||||
QSqlQueryModel model;
|
||||
|
|
@ -273,6 +250,7 @@ class MyModel : public QSqlQueryModel
|
|||
{
|
||||
public:
|
||||
QVariant data(const QModelIndex &item, int role) const override;
|
||||
void fetchModel();
|
||||
|
||||
int m_specialColumnNo;
|
||||
};
|
||||
|
|
@ -289,20 +267,6 @@ QVariant MyModel::data(const QModelIndex &item, int role) const
|
|||
|
||||
void QSqlTableModel_snippets()
|
||||
{
|
||||
//! [24]
|
||||
QSqlTableModel *model = new QSqlTableModel(parentObject, database);
|
||||
model->setTable("employee");
|
||||
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
|
||||
model->select();
|
||||
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
|
||||
model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
|
||||
|
||||
QTableView *view = new QTableView;
|
||||
view->setModel(model);
|
||||
view->hideColumn(0); // don't show the ID
|
||||
view->show();
|
||||
//! [24]
|
||||
|
||||
{
|
||||
//! [25]
|
||||
QSqlTableModel model;
|
||||
|
|
@ -557,7 +521,7 @@ public:
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
QSqlDatabase_snippets();
|
||||
QSqlField_snippets();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,387 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2020 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the documentation of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** BSD License Usage
|
||||
** Alternatively, you may use this file under the terms of the BSD license
|
||||
** as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of The Qt Company Ltd nor the names of its
|
||||
** contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
//! [16]
|
||||
QSqlQueryModel *model = new QSqlQueryModel;
|
||||
model->setQuery("SELECT name, salary FROM employee");
|
||||
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
|
||||
model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
|
||||
//! [17]
|
||||
QTableView *view = new QTableView;
|
||||
//! [17] //! [18]
|
||||
view->setModel(model);
|
||||
//! [18] //! [19]
|
||||
view->show();
|
||||
//! [16] //! [19] //! [20]
|
||||
view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
//! [20]
|
||||
}
|
||||
|
||||
//! [21]
|
||||
QSqlQueryModel model;
|
||||
model.setQuery("SELECT name, salary FROM employee");
|
||||
int salary = model.record(4).value("salary").toInt();
|
||||
//! [21]
|
||||
Q_UNUSED(salary);
|
||||
|
||||
{
|
||||
//! [22]
|
||||
int salary = model.data(model.index(4, 1)).toInt();
|
||||
//! [22]
|
||||
Q_UNUSED(salary);
|
||||
}
|
||||
|
||||
for (int row = 0; row < model.rowCount(); ++row) {
|
||||
for (int col = 0; col < model.columnCount(); ++col) {
|
||||
qDebug() << model.data(model.index(row, col));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MyModel : public QSqlQueryModel
|
||||
{
|
||||
public:
|
||||
QVariant data(const QModelIndex &item, int role) const override;
|
||||
void fetchModel();
|
||||
|
||||
int m_specialColumnNo;
|
||||
};
|
||||
|
||||
//! [23]
|
||||
QVariant MyModel::data(const QModelIndex &item, int role) const
|
||||
{
|
||||
if (item.column() == m_specialColumnNo) {
|
||||
// handle column separately
|
||||
}
|
||||
return QSqlQueryModel::data(item, role);
|
||||
}
|
||||
//! [23]
|
||||
|
||||
void QSqlTableModel_snippets()
|
||||
{
|
||||
//! [24]
|
||||
QSqlTableModel *model = new QSqlTableModel;
|
||||
model->setTable("employee");
|
||||
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
|
||||
model->select();
|
||||
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
|
||||
model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
|
||||
|
||||
QTableView *view = new QTableView;
|
||||
view->setModel(model);
|
||||
view->hideColumn(0); // don't show the ID
|
||||
view->show();
|
||||
//! [24]
|
||||
|
||||
{
|
||||
//! [25]
|
||||
QSqlTableModel model;
|
||||
model.setTable("employee");
|
||||
model.select();
|
||||
int salary = model.record(4).value("salary").toInt();
|
||||
//! [25]
|
||||
}
|
||||
}
|
||||
|
||||
void sql_intro_snippets()
|
||||
{
|
||||
{
|
||||
//! [26]
|
||||
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
|
||||
db.setHostName("bigblue");
|
||||
db.setDatabaseName("flightdb");
|
||||
db.setUserName("acarlson");
|
||||
db.setPassword("1uTbSbAs");
|
||||
bool ok = db.open();
|
||||
//! [26]
|
||||
Q_UNUSED(ok);
|
||||
}
|
||||
|
||||
{
|
||||
//! [27]
|
||||
QSqlDatabase firstDB = QSqlDatabase::addDatabase("QMYSQL", "first");
|
||||
QSqlDatabase secondDB = QSqlDatabase::addDatabase("QMYSQL", "second");
|
||||
//! [27]
|
||||
}
|
||||
|
||||
{
|
||||
//! [28]
|
||||
QSqlDatabase defaultDB = QSqlDatabase::database();
|
||||
//! [28] //! [29]
|
||||
QSqlDatabase firstDB = QSqlDatabase::database("first");
|
||||
//! [29] //! [30]
|
||||
QSqlDatabase secondDB = QSqlDatabase::database("second");
|
||||
//! [30]
|
||||
}
|
||||
|
||||
{
|
||||
// SELECT1
|
||||
//! [31]
|
||||
QSqlQuery query;
|
||||
query.exec("SELECT name, salary FROM employee WHERE salary > 50000");
|
||||
//! [31]
|
||||
|
||||
//! [32]
|
||||
while (query.next()) {
|
||||
QString name = query.value(0).toString();
|
||||
int salary = query.value(1).toInt();
|
||||
qDebug() << name << salary;
|
||||
}
|
||||
//! [32]
|
||||
}
|
||||
|
||||
{
|
||||
// FEATURE
|
||||
//! [33]
|
||||
QSqlQuery query;
|
||||
int numRows;
|
||||
query.exec("SELECT name, salary FROM employee WHERE salary > 50000");
|
||||
|
||||
QSqlDatabase defaultDB = QSqlDatabase::database();
|
||||
if (defaultDB.driver()->hasFeature(QSqlDriver::QuerySize)) {
|
||||
numRows = query.size();
|
||||
} else {
|
||||
// this can be very slow
|
||||
query.last();
|
||||
numRows = query.at() + 1;
|
||||
}
|
||||
//! [33]
|
||||
}
|
||||
|
||||
{
|
||||
// INSERT1
|
||||
//! [34]
|
||||
QSqlQuery query;
|
||||
query.exec("INSERT INTO employee (id, name, salary) "
|
||||
"VALUES (1001, 'Thad Beaumont', 65000)");
|
||||
//! [34]
|
||||
}
|
||||
|
||||
{
|
||||
// NAMED BINDING
|
||||
//! [35]
|
||||
QSqlQuery query;
|
||||
query.prepare("INSERT INTO employee (id, name, salary) "
|
||||
"VALUES (:id, :name, :salary)");
|
||||
query.bindValue(":id", 1001);
|
||||
query.bindValue(":name", "Thad Beaumont");
|
||||
query.bindValue(":salary", 65000);
|
||||
query.exec();
|
||||
//! [35]
|
||||
}
|
||||
|
||||
{
|
||||
// POSITIONAL BINDING
|
||||
//! [36]
|
||||
QSqlQuery query;
|
||||
query.prepare("INSERT INTO employee (id, name, salary) "
|
||||
"VALUES (?, ?, ?)");
|
||||
query.addBindValue(1001);
|
||||
query.addBindValue("Thad Beaumont");
|
||||
query.addBindValue(65000);
|
||||
query.exec();
|
||||
//! [36]
|
||||
}
|
||||
|
||||
{
|
||||
// UPDATE1
|
||||
//! [37]
|
||||
QSqlQuery query;
|
||||
query.exec("UPDATE employee SET salary = 70000 WHERE id = 1003");
|
||||
//! [37]
|
||||
}
|
||||
|
||||
{
|
||||
// DELETE1
|
||||
//! [38]
|
||||
QSqlQuery query;
|
||||
query.exec("DELETE FROM employee WHERE id = 1007");
|
||||
//! [38]
|
||||
}
|
||||
|
||||
{
|
||||
// TRANSACTION
|
||||
//! [39]
|
||||
QSqlDatabase::database().transaction();
|
||||
QSqlQuery query;
|
||||
query.exec("SELECT id FROM employee WHERE name = 'Torild Halvorsen'");
|
||||
if (query.next()) {
|
||||
int employeeId = query.value(0).toInt();
|
||||
query.exec("INSERT INTO project (id, name, ownerid) "
|
||||
"VALUES (201, 'Manhattan Project', "
|
||||
+ QString::number(employeeId) + ')');
|
||||
}
|
||||
QSqlDatabase::database().commit();
|
||||
//! [39]
|
||||
}
|
||||
|
||||
{
|
||||
// SQLQUERYMODEL1
|
||||
//! [40]
|
||||
QSqlQueryModel model;
|
||||
model.setQuery("SELECT * FROM employee");
|
||||
|
||||
for (int i = 0; i < model.rowCount(); ++i) {
|
||||
int id = model.record(i).value("id").toInt();
|
||||
QString name = model.record(i).value("name").toString();
|
||||
qDebug() << id << name;
|
||||
}
|
||||
//! [40]
|
||||
}
|
||||
|
||||
{
|
||||
// SQLTABLEMODEL1
|
||||
//! [41]
|
||||
QSqlTableModel model;
|
||||
model.setTable("employee");
|
||||
model.setFilter("salary > 50000");
|
||||
model.setSort(2, Qt::DescendingOrder);
|
||||
model.select();
|
||||
|
||||
for (int i = 0; i < model.rowCount(); ++i) {
|
||||
QString name = model.record(i).value("name").toString();
|
||||
int salary = model.record(i).value("salary").toInt();
|
||||
qDebug() << name << salary;
|
||||
}
|
||||
//! [41]
|
||||
}
|
||||
|
||||
{
|
||||
// SQLTABLEMODEL2
|
||||
QSqlTableModel model;
|
||||
model.setTable("employee");
|
||||
|
||||
//! [42]
|
||||
for (int i = 0; i < model.rowCount(); ++i) {
|
||||
QSqlRecord record = model.record(i);
|
||||
double salary = record.value("salary").toInt();
|
||||
salary *= 1.1;
|
||||
record.setValue("salary", salary);
|
||||
model.setRecord(i, record);
|
||||
}
|
||||
model.submitAll();
|
||||
//! [42]
|
||||
|
||||
// SQLTABLEMODEL3
|
||||
int row = 1;
|
||||
int column = 2;
|
||||
//! [43]
|
||||
model.setData(model.index(row, column), 75000);
|
||||
model.submitAll();
|
||||
//! [43]
|
||||
|
||||
// SQLTABLEMODEL4
|
||||
//! [44]
|
||||
model.insertRows(row, 1);
|
||||
model.setData(model.index(row, 0), 1013);
|
||||
model.setData(model.index(row, 1), "Peter Gordon");
|
||||
model.setData(model.index(row, 2), 68500);
|
||||
model.submitAll();
|
||||
//! [44]
|
||||
|
||||
//! [45]
|
||||
model.removeRows(row, 5);
|
||||
//! [45] //! [46]
|
||||
model.submitAll();
|
||||
//! [46]
|
||||
}
|
||||
}
|
||||
|
||||
//! [47]
|
||||
class XyzResult : public QSqlResult
|
||||
{
|
||||
public:
|
||||
XyzResult(const QSqlDriver *driver)
|
||||
: QSqlResult(driver) {}
|
||||
~XyzResult() {}
|
||||
|
||||
protected:
|
||||
QVariant data(int /* index */) override { return QVariant(); }
|
||||
bool isNull(int /* index */) override { return false; }
|
||||
bool reset(const QString & /* query */) override { return false; }
|
||||
bool fetch(int /* index */) override { return false; }
|
||||
bool fetchFirst() override { return false; }
|
||||
bool fetchLast() override { return false; }
|
||||
int size() override { return 0; }
|
||||
int numRowsAffected() override { return 0; }
|
||||
QSqlRecord record() const override { return QSqlRecord(); }
|
||||
};
|
||||
//! [47]
|
||||
|
||||
//! [48]
|
||||
class XyzDriver : public QSqlDriver
|
||||
{
|
||||
public:
|
||||
XyzDriver() {}
|
||||
~XyzDriver() {}
|
||||
|
||||
bool hasFeature(DriverFeature /* feature */) const override { return false; }
|
||||
bool open(const QString & /* db */, const QString & /* user */,
|
||||
const QString & /* password */, const QString & /* host */,
|
||||
int /* port */, const QString & /* options */) override
|
||||
{ return false; }
|
||||
void close() {}
|
||||
QSqlResult *createResult() const override { return new XyzResult(this); }
|
||||
};
|
||||
//! [48]
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
QSqlDatabase_snippets();
|
||||
QSqlField_snippets();
|
||||
QSqlQuery_snippets();
|
||||
QSqlQueryModel_snippets();
|
||||
QSqlTableModel_snippets();
|
||||
|
||||
XyzDriver driver;
|
||||
XyzResult result(&driver);
|
||||
}
|
||||
|
|
@ -459,7 +459,7 @@
|
|||
SQL result must get a new handle after each call to any of QSqlResult
|
||||
fetch functions. Example:
|
||||
|
||||
\snippet code/doc_src_sql-driver.cpp 36
|
||||
\snippet code/doc_src_sql-driver_snippet.cpp 36
|
||||
|
||||
While reading the results of a forward-only query with PostgreSQL,
|
||||
the database connection cannot be used to execute other queries.
|
||||
|
|
|
|||
|
|
@ -480,15 +480,15 @@
|
|||
|
||||
The following example creates a view based on an SQL data model:
|
||||
|
||||
\snippet sqldatabase/sqldatabase.cpp 17
|
||||
\snippet sqldatabase/sqldatabase.cpp 18
|
||||
\snippet sqldatabase/sqldatabase.cpp 19
|
||||
\snippet sqldatabase/sqldatabase_snippet.cpp 17
|
||||
\snippet sqldatabase/sqldatabase_snippet.cpp 18
|
||||
\snippet sqldatabase/sqldatabase_snippet.cpp 19
|
||||
|
||||
If the model is a read-write model (e.g., QSqlTableModel), the
|
||||
view lets the user edit the fields. You can disable this by
|
||||
calling
|
||||
|
||||
\snippet sqldatabase/sqldatabase.cpp 20
|
||||
\snippet sqldatabase/sqldatabase_snippet.cpp 20
|
||||
|
||||
You can use the same model as a data source for multiple views.
|
||||
If the user edits the model through one of the views, the other
|
||||
|
|
|
|||
|
|
@ -561,7 +561,7 @@ QStringList QSqlDatabase::drivers()
|
|||
and don't want to compile it as a plugin.
|
||||
|
||||
Example:
|
||||
\snippet code/src_sql_kernel_qsqldatabase.cpp 2
|
||||
\snippet code/src_sql_kernel_qsqldatabase_snippet.cpp 2
|
||||
|
||||
QSqlDatabase takes ownership of the \a creator pointer, so you
|
||||
mustn't delete it yourself.
|
||||
|
|
@ -1271,9 +1271,7 @@ bool QSqlDatabase::isDriverAvailable(const QString& name)
|
|||
application. For example, you can create a PostgreSQL connection
|
||||
with your own QPSQL driver like this:
|
||||
|
||||
\snippet code/src_sql_kernel_qsqldatabase.cpp 5
|
||||
\codeline
|
||||
\snippet code/src_sql_kernel_qsqldatabase.cpp 6
|
||||
\snippet code/src_sql_kernel_qsqldatabase_snippet.cpp 6
|
||||
|
||||
The above code sets up a PostgreSQL connection and instantiates a
|
||||
QPSQLDriver object. Next, addDatabase() is called to add the
|
||||
|
|
@ -1292,7 +1290,7 @@ bool QSqlDatabase::isDriverAvailable(const QString& name)
|
|||
client library. Make sure the client library is in your linker's
|
||||
search path, and add lines like these to your \c{.pro} file:
|
||||
|
||||
\snippet code/src_sql_kernel_qsqldatabase.cpp 7
|
||||
\snippet code/src_sql_kernel_qsqldatabase_snippet.cpp 7
|
||||
|
||||
The method described works for all the supplied drivers. The only
|
||||
difference will be in the driver constructor arguments. Here is a
|
||||
|
|
|
|||
|
|
@ -414,7 +414,7 @@ bool QSqlQuery::exec(const QString& query)
|
|||
The fields are numbered from left to right using the text of the
|
||||
\c SELECT statement, e.g. in
|
||||
|
||||
\snippet code/src_sql_kernel_qsqlquery.cpp 0
|
||||
\snippet code/src_sql_kernel_qsqlquery_snippet.cpp 0
|
||||
|
||||
field 0 is \c forename and field 1 is \c
|
||||
surname. Using \c{SELECT *} is not recommended because the order
|
||||
|
|
@ -1044,7 +1044,7 @@ bool QSqlQuery::exec()
|
|||
|
||||
The example above inserts four new rows into \c myTable:
|
||||
|
||||
\snippet code/src_sql_kernel_qsqlquery.cpp 3
|
||||
\snippet code/src_sql_kernel_qsqlquery_snippet.cpp 3
|
||||
|
||||
To bind NULL values, a null QVariant of the relevant type has to be
|
||||
added to the bound QVariantList; for example, \c
|
||||
|
|
|
|||
|
|
@ -1022,7 +1022,7 @@ bool QSqlResult::nextResult()
|
|||
|
||||
This snippet returns the handle for PostgreSQL or MySQL:
|
||||
|
||||
\snippet code/src_sql_kernel_qsqlresult.cpp 2
|
||||
\snippet code/src_sql_kernel_qsqlresult_snippet.cpp 2
|
||||
|
||||
\sa QSqlDriver::handle()
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -113,7 +113,7 @@ int QSqlQueryModelPrivate::columnInQuery(int modelColumn) const
|
|||
the lower-level QSqlQuery and can be used to provide data to
|
||||
view classes such as QTableView. For example:
|
||||
|
||||
\snippet sqldatabase/sqldatabase.cpp 16
|
||||
\snippet sqldatabase/sqldatabase_snippet.cpp 16
|
||||
|
||||
We set the model's query, then we set up the labels displayed in
|
||||
the view header.
|
||||
|
|
|
|||
|
|
@ -211,7 +211,7 @@ bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement,
|
|||
lower-level QSqlQuery and can be used to provide data to view
|
||||
classes such as QTableView. For example:
|
||||
|
||||
\snippet sqldatabase/sqldatabase.cpp 24
|
||||
\snippet sqldatabase/sqldatabase_snippet.cpp 24
|
||||
|
||||
We set the SQL table's name and the edit strategy, then we set up
|
||||
the labels displayed in the view header. The edit strategy
|
||||
|
|
|
|||
|
|
@ -172,6 +172,13 @@ qtConfig(network) {
|
|||
qtConfig(sql) {
|
||||
SUBDIRS += src_sql
|
||||
src_plugins.depends += src_sql
|
||||
|
||||
contains(QT_CONFIG, private_tests) {
|
||||
src_sql_doc_snippets.subdir = sql/doc/snippets
|
||||
src_sql_doc_snippets.target = sub-sql-doc-snippets
|
||||
src_sql_doc_snippets.depends = src_sql
|
||||
SUBDIRS += src_sql_doc_snippets
|
||||
}
|
||||
}
|
||||
qtConfig(xml): SUBDIRS += src_xml
|
||||
qtConfig(testlib): SUBDIRS += src_testlib
|
||||
|
|
|
|||
Loading…
Reference in New Issue