[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[651] | 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
| 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | /*!
|
---|
| 43 | \group database
|
---|
| 44 | \title Database Classes
|
---|
| 45 |
|
---|
| 46 | \brief Database related classes, e.g. for SQL databases.
|
---|
| 47 | */
|
---|
| 48 |
|
---|
| 49 | /*!
|
---|
| 50 | \page sql-programming.html
|
---|
| 51 | \title SQL Programming
|
---|
| 52 | \nextpage Connecting to Databases
|
---|
| 53 |
|
---|
| 54 | \brief Database integration for Qt applications.
|
---|
| 55 |
|
---|
| 56 | This overview assumes that you have at least a basic knowledge of
|
---|
| 57 | SQL. You should be able to understand simple \c SELECT, \c
|
---|
| 58 | INSERT, \c UPDATE, and \c DELETE statements. Although the \l
|
---|
| 59 | QSqlTableModel class provides an interface to database browsing
|
---|
| 60 | and editing that does not require a knowledge of SQL, a basic
|
---|
| 61 | understanding of SQL is highly recommended. A standard text
|
---|
| 62 | covering SQL databases is \e {An Introduction to Database Systems}
|
---|
| 63 | (7th Ed.) by C. J. Date, ISBN 0201385902.
|
---|
| 64 |
|
---|
| 65 | \section1 Topics:
|
---|
| 66 |
|
---|
| 67 | \list
|
---|
| 68 | \o \l{Database Classes}
|
---|
| 69 | \o \l{Connecting to Databases}
|
---|
| 70 | \list
|
---|
| 71 | \o \l{SQL Database Drivers}
|
---|
| 72 | \endlist
|
---|
| 73 | \o \l{Executing SQL Statements}
|
---|
| 74 | \list
|
---|
| 75 | \o \l{Recommended Use of Data Types in Databases}
|
---|
| 76 | \endlist
|
---|
| 77 | \o \l{Using the SQL Model Classes}
|
---|
| 78 | \o \l{Presenting Data in a Table View}
|
---|
| 79 | \o \l{Creating Data-Aware Forms}
|
---|
| 80 | \endlist
|
---|
| 81 |
|
---|
| 82 | \section1 Database Classes
|
---|
| 83 |
|
---|
| 84 | These classes provide access to SQL databases.
|
---|
| 85 |
|
---|
| 86 | \annotatedlist database
|
---|
| 87 |
|
---|
| 88 | The SQL classes are divided into three layers:
|
---|
| 89 |
|
---|
| 90 | \section2 Driver Layer
|
---|
| 91 |
|
---|
| 92 | This comprises the classes QSqlDriver, QSqlDriverCreator<T>,
|
---|
| 93 | QSqlDriverCreatorBase, QSqlDriverPlugin, and QSqlResult.
|
---|
| 94 |
|
---|
| 95 | This layer provides the low-level bridge between the specific databases
|
---|
| 96 | and the SQL API layer. See \l{SQL Database Drivers} for more information.
|
---|
| 97 |
|
---|
| 98 | \section2 SQL API Layer
|
---|
| 99 |
|
---|
| 100 | These classes provide access to databases. Connections
|
---|
| 101 | are made using the QSqlDatabase class. Database
|
---|
| 102 | interaction is achieved by using the QSqlQuery class.
|
---|
| 103 | In addition to QSqlDatabase and QSqlQuery, the SQL API
|
---|
| 104 | layer is supported by QSqlError, QSqlField, QSqlIndex,
|
---|
| 105 | and QSqlRecord.
|
---|
| 106 |
|
---|
| 107 | \section2 User Interface Layer
|
---|
| 108 |
|
---|
| 109 | These classes link the data from a database to data-aware widgets.
|
---|
| 110 | They include QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel.
|
---|
| 111 | These classes are designed to work with Qt's
|
---|
| 112 | \l{Model/View Programming}{model/view framework}.
|
---|
| 113 |
|
---|
| 114 | Note that to use any of these classes, a QCoreApplication object
|
---|
| 115 | must have been instantiated first.
|
---|
| 116 | */
|
---|
| 117 |
|
---|
| 118 | /*!
|
---|
| 119 | \page sql-connecting.html
|
---|
| 120 | \title Connecting to Databases
|
---|
| 121 |
|
---|
| 122 | \contentspage SQL Programming
|
---|
| 123 | \previouspage SQL Programming
|
---|
| 124 | \nextpage Executing SQL Statements
|
---|
| 125 |
|
---|
| 126 | To access a database with QSqlQuery or QSqlQueryModel, create and
|
---|
| 127 | open one or more database connections. Database connections are
|
---|
| 128 | normally identified by connection name, \e{not} by database name.
|
---|
| 129 | You can have multiple connections to the same database.
|
---|
| 130 | QSqlDatabase also supports the concept of a \e{default}
|
---|
| 131 | connection, which is an unnamed connection. When calling QSqlQuery
|
---|
| 132 | or QSqlQueryModel member functions that take a connection name
|
---|
| 133 | argument, if you don't pass a connection name, the default
|
---|
| 134 | connection will be used. Creating a default connection is
|
---|
| 135 | convenient when your application only requires one database
|
---|
| 136 | connection.
|
---|
| 137 |
|
---|
| 138 | Note the difference between creating a connection and opening it.
|
---|
| 139 | Creating a connection involves creating an instance of class
|
---|
| 140 | QSqlDatabase. The connection is not usable until it is opened. The
|
---|
| 141 | following snippet shows how to create a \e{default} connection
|
---|
| 142 | and then open it:
|
---|
| 143 |
|
---|
| 144 | \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 26
|
---|
| 145 |
|
---|
| 146 | The first line creates the connection object, and the last line
|
---|
| 147 | opens it for use. In between, we initialize some connection
|
---|
| 148 | information, including the \l{QSqlDatabase::setDatabaseName()}
|
---|
| 149 | {database name}, the \l{QSqlDatabase::setHostName()} {host name},
|
---|
| 150 | the \l{QSqlDatabase::setUserName()} {user name}, and the
|
---|
| 151 | \l{QSqlDatabase::setPassword()} {password}. In this case, we are
|
---|
| 152 | connecting to the MySQL database \c{flightdb} on the host
|
---|
| 153 | \c{bigblue}. The \c{"QMYSQL"} argument to
|
---|
| 154 | \l{QSqlDatabase::addDatabase()} {addDatabase()} specifies the type
|
---|
| 155 | of database driver to use for the connection. The set of database
|
---|
| 156 | drivers included with Qt are shown in the table of \l{SQL Database
|
---|
| 157 | Drivers#Supported Databases} {supported database drivers}.
|
---|
| 158 |
|
---|
| 159 | The connection in the snippet will be the \e{default} connection,
|
---|
| 160 | because we don't pass the second argument to
|
---|
| 161 | \l{QSqlDatabase::addDatabase()} {addDatabase()}, which is the
|
---|
| 162 | connection name. For example, here we establish two MySQL database
|
---|
| 163 | connections named \c{"first"} and \c{"second"}:
|
---|
| 164 |
|
---|
| 165 | \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 27
|
---|
| 166 |
|
---|
| 167 | After these connections have been initialized, \l{QSqlDatabase::}
|
---|
| 168 | {open()} for each one to establish the live connections. If the
|
---|
| 169 | \l{QSqlDatabase::} {open()} fails, it returns false. In that case,
|
---|
| 170 | call QSqlDatabase::lastError() to get error information.
|
---|
| 171 |
|
---|
| 172 | Once a connection is established, we can call the static function
|
---|
| 173 | QSqlDatabase::database() from anywhere with a connection name to
|
---|
| 174 | get a pointer to that database connection. If we don't pass a
|
---|
| 175 | connection name, it will return the default connection. For
|
---|
| 176 | example:
|
---|
| 177 |
|
---|
| 178 | \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 28
|
---|
| 179 | \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 29
|
---|
| 180 | \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 30
|
---|
| 181 |
|
---|
| 182 | To remove a database connection, first close the database using
|
---|
| 183 | QSqlDatabase::close(), then remove it using the static method
|
---|
| 184 | QSqlDatabase::removeDatabase().
|
---|
| 185 | */
|
---|
| 186 |
|
---|
| 187 | /*!
|
---|
| 188 | \page sql-sqlstatements.html
|
---|
| 189 | \title Executing SQL Statements
|
---|
| 190 |
|
---|
| 191 | \previouspage Connecting to Databases
|
---|
| 192 | \contentspage SQL Programming
|
---|
| 193 | \nextpage Using the SQL Model Classes
|
---|
| 194 |
|
---|
| 195 |
|
---|
| 196 | The QSqlQuery class provides an interface for executing SQL
|
---|
| 197 | statements and navigating through the result set of a query.
|
---|
| 198 |
|
---|
| 199 | The QSqlQueryModel and QSqlTableModel classes described in the
|
---|
| 200 | next section provide a higher-level interface for accessing
|
---|
| 201 | databases. If you are unfamiliar with SQL, you might want to skip
|
---|
| 202 | directly to the next section (\l{Using the SQL Model Classes}).
|
---|
| 203 |
|
---|
| 204 | \section2 Executing a Query
|
---|
| 205 |
|
---|
| 206 | To execute an SQL statement, simply create a QSqlQuery object and
|
---|
| 207 | call QSqlQuery::exec() like this:
|
---|
| 208 |
|
---|
| 209 | \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 31
|
---|
| 210 |
|
---|
| 211 | The QSqlQuery constructor accepts an optional QSqlDatabase object
|
---|
| 212 | that specifies which database connection to use. In the example
|
---|
| 213 | above, we don't specify any connection, so the default connection
|
---|
| 214 | is used.
|
---|
| 215 |
|
---|
| 216 | If an error occurs, \l{QSqlQuery::exec()}{exec()} returns false.
|
---|
| 217 | The error is then available as QSqlQuery::lastError().
|
---|
| 218 |
|
---|
| 219 | \section2 Navigating the Result Set
|
---|
| 220 |
|
---|
| 221 | QSqlQuery provides access to the result set one record at a time.
|
---|
| 222 | After the call to \l{QSqlQuery::exec()}{exec()}, QSqlQuery's
|
---|
| 223 | internal pointer is located one position \e{before} the first
|
---|
| 224 | record. We must call QSqlQuery::next() once to advance to the
|
---|
| 225 | first record, then \l{QSqlQuery::next()}{next()} again repeatedly
|
---|
| 226 | to access the other records, until it returns false. Here's a
|
---|
| 227 | typical loop that iterates over all the records in order:
|
---|
| 228 |
|
---|
| 229 | \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 32
|
---|
| 230 |
|
---|
| 231 | The QSqlQuery::value() function returns the value of a field in
|
---|
| 232 | the current record. Fields are specified as zero-based indexes.
|
---|
| |
---|