Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
src_sql_kernel_qsqldatabase.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{
11// WRONG
13QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
14QSqlDatabase::removeDatabase("sales"); // will output a warning
15// "db" is now a dangling invalid database connection,
16// "query" contains an invalid result set
18}
19
21{
23{
25 QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
26}
27// Both "db" and "query" are destroyed because they are out of scope
28QSqlDatabase::removeDatabase("sales"); // correct
30}
31
33{
35// ...
37db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=myaccessfile.mdb");
38if (db.open()) {
39 // success!
40}
41// ...
43}
44
45// ...
46// MySQL connection
48{
51db.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
52if (!db.open()) {
53 db.setConnectOptions(); // clears the connect option string
54 // ...
55}
56// ...
57// PostgreSQL connection
58db.setConnectOptions("requiressl=1"); // enable PostgreSQL SSL connections
59if (!db.open()) {
60 db.setConnectOptions(); // clear options
61 // ...
62}
63// ...
64// ODBC connection
65db.setConnectOptions("SQL_ATTR_ACCESS_MODE=SQL_MODE_READ_ONLY;SQL_ATTR_TRACE=SQL_OPT_TRACE_ON"); // set ODBC options
66if (!db.open()) {
67 db.setConnectOptions(); // don't try to set this option
68 // ...
69}
70}
72
74{
77qDebug() << db.isValid(); // Returns false
78
79db = QSqlDatabase::database("sales");
80qDebug() << db.isValid(); // Returns \c true if "sales" connection exists
81
83qDebug() << db.isValid(); // Returns false
85}
The QSqlDatabase class handles a connection to a database.
static void removeDatabase(const QString &connectionName)
\threadsafe
static QSqlDatabase addDatabase(const QString &type, const QString &connectionName=QLatin1StringView(defaultConnection))
\threadsafe
static QSqlDatabase database(const QString &connectionName=QLatin1StringView(defaultConnection), bool open=true)
\threadsafe
The QSqlQuery class provides a means of executing and manipulating SQL statements.
Definition qsqlquery.h:24
#define qDebug
[1]
Definition qlogging.h:164
GLenum query
QMimeDatabase db
[0]
void dbQdebug()
[4]
void removeDatabase()
void openDatabase()