1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QSqlDatabase Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">  </td><td class="postheader" valign="center"><a href="../pyqt4ref.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QSqlDatabase Class Reference<br /><sup><sup>[<a href="qtsql.html">QtSql</a> module]</sup></sup></h1><p>The QSqlDatabase class represents a connection to a database. <a href="#details">More...</a></p>
<h3>Methods</h3><ul><li><div class="fn" /><b><a href="qsqldatabase.html#QSqlDatabase">__init__</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsqldatabase.html#QSqlDatabase-2">__init__</a></b> (<i>self</i>, QSqlDatabase <i>other</i>)</li><li><div class="fn" /><b><a href="qsqldatabase.html#QSqlDatabase-3">__init__</a></b> (<i>self</i>, QString <i>type</i>)</li><li><div class="fn" /><b><a href="qsqldatabase.html#QSqlDatabase-4">__init__</a></b> (<i>self</i>, QSqlDriver <i>driver</i>)</li><li><div class="fn" /><b><a href="qsqldatabase.html#close">close</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqldatabase.html#commit">commit</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsqldatabase.html#connectOptions">connectOptions</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsqldatabase.html#databaseName">databaseName</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlDriver <b><a href="qsqldatabase.html#driver">driver</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsqldatabase.html#driverName">driverName</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlQuery <b><a href="qsqldatabase.html#exec">exec_</a></b> (<i>self</i>, QString <i>query</i> = QString())</li><li><div class="fn" />QString <b><a href="qsqldatabase.html#hostName">hostName</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqldatabase.html#isOpen">isOpen</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqldatabase.html#isOpenError">isOpenError</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqldatabase.html#isValid">isValid</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlError <b><a href="qsqldatabase.html#lastError">lastError</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqldatabase.html#open">open</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqldatabase.html#open-2">open</a></b> (<i>self</i>, QString <i>user</i>, QString <i>password</i>)</li><li><div class="fn" />QString <b><a href="qsqldatabase.html#password">password</a></b> (<i>self</i>)</li><li><div class="fn" />int <b><a href="qsqldatabase.html#port">port</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlIndex <b><a href="qsqldatabase.html#primaryIndex">primaryIndex</a></b> (<i>self</i>, QString <i>tablename</i>)</li><li><div class="fn" />QSqlRecord <b><a href="qsqldatabase.html#record">record</a></b> (<i>self</i>, QString <i>tablename</i>)</li><li><div class="fn" />bool <b><a href="qsqldatabase.html#rollback">rollback</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsqldatabase.html#setConnectOptions">setConnectOptions</a></b> (<i>self</i>, QString <i>options</i> = QString())</li><li><div class="fn" /><b><a href="qsqldatabase.html#setDatabaseName">setDatabaseName</a></b> (<i>self</i>, QString <i>name</i>)</li><li><div class="fn" /><b><a href="qsqldatabase.html#setHostName">setHostName</a></b> (<i>self</i>, QString <i>host</i>)</li><li><div class="fn" /><b><a href="qsqldatabase.html#setPassword">setPassword</a></b> (<i>self</i>, QString <i>password</i>)</li><li><div class="fn" /><b><a href="qsqldatabase.html#setPort">setPort</a></b> (<i>self</i>, int <i>p</i>)</li><li><div class="fn" /><b><a href="qsqldatabase.html#setUserName">setUserName</a></b> (<i>self</i>, QString <i>name</i>)</li><li><div class="fn" />QStringList <b><a href="qsqldatabase.html#tables">tables</a></b> (<i>self</i>, QSql.TableType <i>type</i> = QSql.Tables)</li><li><div class="fn" />bool <b><a href="qsqldatabase.html#transaction">transaction</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsqldatabase.html#userName">userName</a></b> (<i>self</i>)</li></ul><h3>Static Methods</h3><ul><li><div class="fn" />QSqlDatabase <b><a href="qsqldatabase.html#addDatabase">addDatabase</a></b> (QString <i>type</i>, QString <i>connectionName</i> = QLatin1String(QSqlDatabase.defaultConnection))</li><li><div class="fn" />QSqlDatabase <b><a href="qsqldatabase.html#addDatabase-2">addDatabase</a></b> (QSqlDriver <i>driver</i>, QString <i>connectionName</i> = QLatin1String(QSqlDatabase.defaultConnection))</li><li><div class="fn" />QSqlDatabase <b><a href="qsqldatabase.html#cloneDatabase">cloneDatabase</a></b> (QSqlDatabase <i>other</i>, QString <i>connectionName</i>)</li><li><div class="fn" />QStringList <b><a href="qsqldatabase.html#connectionNames">connectionNames</a></b> ()</li><li><div class="fn" />bool <b><a href="qsqldatabase.html#contains">contains</a></b> (QString <i>connectionName</i> = QLatin1String(QSqlDatabase.defaultConnection))</li><li><div class="fn" />QSqlDatabase <b><a href="qsqldatabase.html#database">database</a></b> (QString <i>connectionName</i> = QLatin1String(QSqlDatabase.defaultConnection), bool <i>open</i> = True)</li><li><div class="fn" />QStringList <b><a href="qsqldatabase.html#drivers">drivers</a></b> ()</li><li><div class="fn" />bool <b><a href="qsqldatabase.html#isDriverAvailable">isDriverAvailable</a></b> (QString <i>name</i>)</li><li><div class="fn" /><b><a href="qsqldatabase.html#registerSqlDriver">registerSqlDriver</a></b> (QString <i>name</i>, QSqlDriverCreatorBase <i>creator</i>)</li><li><div class="fn" /><b><a href="qsqldatabase.html#removeDatabase">removeDatabase</a></b> (QString <i>connectionName</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QSqlDatabase class represents a connection to a database.</p>
<p>The QSqlDatabase class provides an abstract interface for accessing database backends. It relies on database-specific <a href="qsqldriver.html">QSqlDriver</a>s to actually access and manipulate data.</p>
<p>The following code shows how to initialize a connection:</p>
<pre>
QSqlDatabase db = QSqlDatabase.addDatabase("QPSQL");
db.setHostName("acidalia");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");
bool ok = db.open();
</pre>
<p>Once a QSqlDatabase object has been created you can set the connection parameters with <a href="qsqldatabase.html#setDatabaseName">setDatabaseName</a>(), <a href="qsqldatabase.html#setUserName">setUserName</a>(), <a href="qsqldatabase.html#setPassword">setPassword</a>(), <a href="qsqldatabase.html#setHostName">setHostName</a>(), <a href="qsqldatabase.html#setPort">setPort</a>(), and <a href="qsqldatabase.html#setConnectOptions">setConnectOptions</a>(). Once the parameters have been set up you can call <a href="qsqldatabase.html#open">open</a>() to open the connection.</p>
<p>The connection defined above is a nameless connection. If is the default connection and can be accessed using <a href="qsqldatabase.html#database">database</a>() later on:</p>
<pre>
QSqlDatabase db = QSqlDatabase.database();
</pre>
<p>To make programming more convenient, QSqlDatabase is a value class. Any changes done to a database connection through one QSqlDatabase object will affect other QSqlDatabase objects representing the same connection. Call cloneConnection() if you want to create an independent database connection based on an existing one.</p>
<p>If you need multiple database connections simultaneously, specify an arbitrary name to <a href="qsqldatabase.html#addDatabase">addDatabase</a>() and <a href="qsqldatabase.html#database">database</a>(). Call <a href="qsqldatabase.html#removeDatabase">removeDatabase</a>() to remove connections. QSqlDatabase will output a warning if you try to remove a connection referenced by other QSqlDatabase objects. Use <a href="qsqldatabase.html#contains">contains</a>() to see if a given connection name is in the list of connections.</p>
<p>Once a connection is established you can see what tables the database offers with <a href="qsqldatabase.html#tables">tables</a>(), find the primary index for a table with <a href="qsqldatabase.html#primaryIndex">primaryIndex</a>(), get meta-information about a table's fields (e.g., their names) with <a href="qsqldatabase.html#record">record</a>(), and execute a query with <a href="qsqldatabase.html#exec">exec_</a>().</p>
<p>If transactions are supported, you can use <a href="qsqldatabase.html#transaction">transaction</a>() to start a transaction, and then <a href="qsqldatabase.html#commit">commit</a>() or <a href="qsqldatabase.html#rollback">rollback</a>() to complete it. You can find out whether transactions are supported using <a href="qsqldriver.html#hasFeature">QSqlDriver.hasFeature</a>().</p>
<p>If an error occurred, it is given by <a href="qsqldatabase.html#lastError">lastError</a>().</p>
<p>The names of the underlying SQL drivers are available from <a href="qsqldatabase.html#drivers">drivers</a>(); you can check for a particular driver with <a href="qsqldatabase.html#isDriverAvailable">isDriverAvailable</a>(). If you have created your own custom driver, you can register it with <a href="qsqldatabase.html#registerSqlDriver">registerSqlDriver</a>().</p>
<p>See also <a href="qsqldriver.html">QSqlDriver</a> and <a href="qsqlquery.html">QSqlQuery</a>.</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QSqlDatabase" />QSqlDatabase.__init__ (<i>self</i>)</h3><p>Creates an empty, invalid <a href="qsqldatabase.html">QSqlDatabase</a> object. Use <a href="qsqldatabase.html#addDatabase">addDatabase</a>(), <a href="qsqldatabase.html#removeDatabase">removeDatabase</a>(), and <a href="qsqldatabase.html#database">database</a>() to get valid <a href="qsqldatabase.html">QSqlDatabase</a> objects.</p>
<h3 class="fn"><a name="QSqlDatabase-2" />QSqlDatabase.__init__ (<i>self</i>, <a href="qsqldatabase.html">QSqlDatabase</a> <i>other</i>)</h3><p>Creates a copy of <i>other</i>.</p>
<h3 class="fn"><a name="QSqlDatabase-3" />QSqlDatabase.__init__ (<i>self</i>, <a href="qstring.html">QString</a> <i>type</i>)</h3><p>Creates a <a href="qsqldatabase.html">QSqlDatabase</a> connection that uses the driver referred to by <i>type</i>. If the <i>type</i> is not recognized, the database connection will have no functionality.</p>
<p>The currently available driver types are:</p>
<table align="center" border="0" cellpadding="2" cellspacing="1">
<tr bgcolor="#A2C511" valign="top">
<th>Driver Type</th>
<th>Description</th>
</tr>
<tr bgcolor="#F0F0F0" valign="top">
<td>QDB2</td>
<td>IBM DB2</td>
</tr>
<tr bgcolor="#E0E0E0" valign="top">
<td>QIBASE</td>
<td>Borland InterBase Driver</td>
</tr>
<tr bgcolor="#F0F0F0" valign="top">
<td>QMYSQL</td>
<td>MySQL Driver</td>
</tr>
<tr bgcolor="#E0E0E0" valign="top">
<td>QOCI</td>
<td>Oracle Call Interface Driver</td>
</tr>
<tr bgcolor="#F0F0F0" valign="top">
<td>QODBC</td>
<td>ODBC Driver (includes Microsoft SQL Server)</td>
</tr>
<tr bgcolor="#E0E0E0" valign="top">
<td>QPSQL</td>
<td>PostgreSQL Driver</td>
</tr>
<tr bgcolor="#F0F0F0" valign="top">
<td>QSQLITE</td>
<td><a href="3rdparty.html#sqlite">SQLite</a> version 3 or above</td>
</tr>
<tr bgcolor="#E0E0E0" valign="top">
<td>QSQLITE2</td>
<td><a href="3rdparty.html#sqlite">SQLite</a> version 2</td>
</tr>
<tr bgcolor="#F0F0F0" valign="top">
<td>QTDS</td>
<td>Sybase Adaptive Server</td>
</tr>
</table>
<br />
<br />
<p>Additional third party drivers, including your own custom drivers, can be loaded dynamically.</p>
<p>See also <a href="sql-driver.html">SQL Database Drivers</a>, <a href="qsqldatabase.html#registerSqlDriver">registerSqlDriver</a>(), and <a href="qsqldatabase.html#drivers">drivers</a>().</p>
<h3 class="fn"><a name="QSqlDatabase-4" />QSqlDatabase.__init__ (<i>self</i>, <a href="qsqldriver.html">QSqlDriver</a> <i>driver</i>)</h3><p>Creates a database connection using the given <i>driver</i>.</p>
<h3 class="fn"><a name="addDatabase" /><a href="qsqldatabase.html">QSqlDatabase</a> QSqlDatabase.addDatabase (<a href="qstring.html">QString</a> <i>type</i>, <a href="qstring.html">QString</a> <i>connectionName</i> = QLatin1String(QSqlDatabase.defaultConnection))</h3><p>Adds a database to the list of database connections using the driver <i>type</i> and the connection name <i>connectionName</i>. If there already exists a database connection called <i>connectionName</i>, that connection is removed.</p>
<p>The database connection is referred to by <i>connectionName</i>. The newly added database connection is returned.</p>
<p>If <i>connectionName</i> is not specified, the newly added database connection becomes the default database connection for the application, and subsequent calls to <a href="qsqldatabase.html#database">database</a>() without a database name parameter will return a reference to it. If <i>connectionName</i> is given, use database(<i>connectionName</i>) to retrieve a pointer to the database connection.</p>
<p><b>Warning:</b> If you add a database with the same name as an existing database, the new database will replace the old one. This will happen automatically if you call this function more than once without specifying <i>connectionName</i>.</p>
<p>To make use of the connection, you will need to set it up, for example by calling some or all of <a href="qsqldatabase.html#setDatabaseName">setDatabaseName</a>(), <a href="qsqldatabase.html#setUserName">setUserName</a>(), <a href="qsqldatabase.html#setPassword">setPassword</a>(), <a href="qsqldatabase.html#setHostName">setHostName</a>(), <a href="qsqldatabase.html#setPort">setPort</a>(), and <a href="qsqldatabase.html#setConnectOptions">setConnectOptions</a>(), and then you'll need to <a href="qsqldatabase.html#open">open</a>() the connection.</p>
<p><b>Note:</b> This function is <a href="threads.html#thread-safe">thread-safe</a>.</p>
<p>See also <a href="qsqldatabase.html#database">database</a>() and <a href="qsqldatabase.html#removeDatabase">removeDatabase</a>().</p>
<h3 class="fn"><a name="addDatabase-2" /><a href="qsqldatabase.html">QSqlDatabase</a> QSqlDatabase.addDatabase (<a href="qsqldriver.html">QSqlDriver</a> <i>driver</i>, <a href="qstring.html">QString</a> <i>connectionName</i> = QLatin1String(QSqlDatabase.defaultConnection))</h3><p>This is an overloaded member function, provided for convenience.</p>
<p>This function is useful if you need to set up the database connection and instantiate the driver yourself. If you do this, it is recommended that you include the driver code in your own application. For example, setting up a custom PostgreSQL connection and instantiating the QPSQL driver can be done like this:</p>
<pre>
#include "qtdir/src/sql/drivers/psql/qsql_psql.cpp"
</pre>
<p>(We assume that <tt>qtdir</tt> is the directory where Qt is installed.) This will pull in the code that is needed to use the PostgreSQL client library and to instantiate a QPSQLDriver object, assuming that you have the PostgreSQL headers somewhere in your include search path.</p>
<pre>
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");
...
</pre>
<p>The above code sets up a PostgreSQL connection and instantiates a QPSQLDriver object. Next, <a href="qsqldatabase.html#addDatabase">addDatabase</a>() is called to add the connection to the known connections so that it can be used by the Qt SQL classes. When a driver is instantiated with a connection handle (or set of handles), Qt assumes that you have already opened the database connection.</p>
<p>Remember that you must link your application against the database client library as well. The simplest way to do this is to add lines like the ones below to your <tt>.pro</tt> file:</p>
<pre>
unix:LIBS += -lpq
win32:LIBS += libpqdll.lib
</pre>
<p>You will need to have the client library in your linker's search path.</p>
<p>The method described above will work for all the drivers, the only difference is the arguments the driver constructors take. Below is an overview of the drivers and their constructor arguments.</p>
<table align="center" border="0" cellpadding="2" cellspacing="1">
<tr bgcolor="#A2C511" valign="top">
<th>Driver</th>
<th>Class name</th>
<th>Constructor arguments</th>
<th>File to include</th>
</tr>
<tr bgcolor="#F0F0F0" valign="top">
<td>QPSQL</td>
<td>QPSQLDriver</td>
<td>PGconn *connection</td>
<td><tt>qsql_psql.cpp</tt></td>
</tr>
<tr bgcolor="#E0E0E0" valign="top">
<td>QMYSQL</td>
<td>QMYSQLDriver</td>
<td>MYSQL *connection</td>
<td><tt>qsql_mysql.cpp</tt></td>
</tr>
<tr bgcolor="#F0F0F0" valign="top">
<td>QOCI</td>
<td>QOCIDriver</td>
<td>OCIEnv *environment, OCISvcCtx *serviceContext</td>
<td><tt>qsql_oci.cpp</tt></td>
</tr>
<tr bgcolor="#E0E0E0" valign="top">
<td>QODBC</td>
<td>QODBCDriver</td>
<td>SQLHANDLE environment, SQLHANDLE connection</td>
<td><tt>qsql_odbc.cpp</tt></td>
</tr>
<tr bgcolor="#F0F0F0" valign="top">
<td>QDB2</td>
<td>QDB2</td>
<td>SQLHANDLE environment, SQLHANDLE connection</td>
<td><tt>qsql_db2.cpp</tt></td>
</tr>
<tr bgcolor="#E0E0E0" valign="top">
<td>QTDS</td>
<td>QTDSDriver</td>
<td>LOGINREC *loginRecord, DBPROCESS *dbProcess, const <a href="qstring.html">QString</a> &hostName</td>
<td><tt>qsql_tds.cpp</tt></td>
</tr>
<tr bgcolor="#F0F0F0" valign="top">
<td>QSQLITE</td>
<td>QSQLiteDriver</td>
<td>sqlite *connection</td>
<td><tt>qsql_sqlite.cpp</tt></td>
</tr>
<tr bgcolor="#E0E0E0" valign="top">
<td>QIBASE</td>
<td>QIBaseDriver</td>
<td>isc_db_handle connection</td>
<td><tt>qsql_ibase.cpp</tt></td>
</tr>
</table>
<br />
<br />
<p>The host name (or service name) is needed when constructing the QTDSDriver for creating new connections for internal queries. This is to prevent the simultaneous usage of several <a href="qsqlquery.html">QSqlQuery</a>/<a href="porting4.html#qsqlcursor">QSqlCursor</a> objects from blocking each other.</p>
<p><b>Warning:</b> If you add a database with the same name as an existing database, the new database will replace the old one.</p>
<p><b>Warning:</b> The SQL framework takes ownership of the <i>driver</i> pointer, and it should not be deleted. If you want to explicitly remove the connection, use <a href="qsqldatabase.html#removeDatabase">removeDatabase</a>().</p>
<p>See also <a href="qsqldatabase.html#drivers">drivers</a>().</p>
<h3 class="fn"><a name="cloneDatabase" /><a href="qsqldatabase.html">QSqlDatabase</a> QSqlDatabase.cloneDatabase (<a href="qsqldatabase.html">QSqlDatabase</a> <i>other</i>, <a href="qstring.html">QString</a> <i>connectionName</i>)</h3><p>Clones the database connection <i>other</i> and and stores it as <i>connectionName</i>. All the settings from the original database, e.g. <a href="qsqldatabase.html#databaseName">databaseName</a>(), <a href="qsqldatabase.html#hostName">hostName</a>(), etc., are copied across. Does nothing if <i>other</i> is an invalid database. Returns the newly created database connection.</p>
<p>Note that the connection is not opened, to use it, it is neccessary to call <a href="qsqldatabase.html#open">open</a>() first.</p>
<h3 class="fn"><a name="close" />QSqlDatabase.close (<i>self</i>)</h3><p>Closes the database connection, freeing any resources acquired. This will also affect copies of this <a href="qsqldatabase.html">QSqlDatabase</a> object.</p>
<p>See also <a href="qsqldatabase.html#removeDatabase">removeDatabase</a>().</p>
<h3 class="fn"><a name="commit" />bool QSqlDatabase.commit (<i>self</i>)</h3><p>Commits a transaction to the database if the driver supports transactions and a <a href="qsqldatabase.html#transaction">transaction</a>() has been started. Returns true if the operation succeeded; otherwise returns false.</p>
<p>Note that on some databases, this function will not work if there is an active <a href="qsqlquery.html">QSqlQuery</a> on the database. Use the <a href="qsqldatabase.html#lastError">lastError</a>() function to retrieve database-specific error data about the error that occurred.</p>
<p>See also <a href="qsqldriver.html#hasFeature">QSqlDriver.hasFeature</a>() and <a href="qsqldatabase.html#rollback">rollback</a>().</p>
<h3 class="fn"><a name="connectionNames" /><a href="qstringlist.html">QStringList</a> QSqlDatabase.connectionNames ()</h3><p>Returns a list containing the names of all connections.</p>
<p><b>Note:</b> This function is <a href="threads.html#thread-safe">thread-safe</a>.</p>
<p>See also <a href="qsqldatabase.html#contains">contains</a>() and <a href="qsqldatabase.html#database">database</a>().</p>
<h3 class="fn"><a name="connectOptions" /><a href="qstring.html">QString</a> QSqlDatabase.connectOptions (<i>self</i>)</h3><p>Returns the connection options string used for this connection. The string may be empty.</p>
<p>See also <a href="qsqldatabase.html#setConnectOptions">setConnectOptions</a>().</p>
<h3 class="fn"><a name="contains" />bool QSqlDatabase.contains (<a href="qstring.html">QString</a> <i>connectionName</i> = QLatin1String(QSqlDatabase.defaultConnection))</h3><p>Returns true if the list of database connections contains <i>connectionName</i>; otherwise returns false.</p>
<p><b>Note:</b> This function is <a href="threads.html#thread-safe">thread-safe</a>.</p>
<p>See also <a href="qsqldatabase.html#connectionNames">connectionNames</a>() and <a href="qsqldatabase.html#database">database</a>().</p>
<h3 class="fn"><a name="database" /><a href="qsqldatabase.html">QSqlDatabase</a> QSqlDatabase.database (<a href="qstring.html">QString</a> <i>connectionName</i> = QLatin1String(QSqlDatabase.defaultConnection), bool <i>open</i> = True)</h3><p>Returns the database connection called <i>connectionName</i>. The database connection must have been previously added with <a href="qsqldatabase.html#addDatabase">addDatabase</a>(). If <i>open</i> is true (the default) and the database connection is not already open it is opened now. If no <i>connectionName</i> is specified the default connection is used. If <i>connectionName</i> does not exist in the list of databases, an invalid connection is returned.</p>
<p><b>Note:</b> This function is <a href="threads.html#thread-safe">thread-safe</a>.</p>
<p>See also <a href="qsqldatabase.html#isOpen">isOpen</a>().</p>
<h3 class="fn"><a name="databaseName" /><a href="qstring.html">QString</a> QSqlDatabase.databaseName (<i>self</i>)</h3><p>Returns the connection's database name; it may be empty.</p>
<p>See also <a href="qsqldatabase.html#setDatabaseName">setDatabaseName</a>().</p>
<h3 class="fn"><a name="driver" /><a href="qsqldriver.html">QSqlDriver</a> QSqlDatabase.driver (<i>self</i>)</h3><p>Returns the database driver used to access the database connection.</p>
<p>See also <a href="qsqldatabase.html#addDatabase">addDatabase</a>() and <a href="qsqldatabase.html#drivers">drivers</a>().</p>
<h3 class="fn"><a name="driverName" /><a href="qstring.html">QString</a> QSqlDatabase.driverName (<i>self</i>)</h3><p>Returns the connection's driver name.</p>
<p>See also <a href="qsqldatabase.html#addDatabase">addDatabase</a>() and <a href="qsqldatabase.html#driver">driver</a>().</p>
<h3 class="fn"><a name="drivers" /><a href="qstringlist.html">QStringList</a> QSqlDatabase.drivers ()</h3><p>Returns a list of all the available database drivers.</p>
<p>See also <a href="qsqldatabase.html#registerSqlDriver">registerSqlDriver</a>().</p>
<h3 class="fn"><a name="exec" /><a href="qsqlquery.html">QSqlQuery</a> QSqlDatabase.exec_ (<i>self</i>, <a href="qstring.html">QString</a> <i>query</i> = QString())</h3><p>Executes a SQL statement on the database and returns a <a href="qsqlquery.html">QSqlQuery</a> object. Use <a href="qsqldatabase.html#lastError">lastError</a>() to retrieve error information. If <i>query</i> is empty, an empty, invalid query is returned and <a href="qsqldatabase.html#lastError">lastError</a>() is not affected.</p>
<p>See also <a href="qsqlquery.html">QSqlQuery</a> and <a href="qsqldatabase.html#lastError">lastError</a>().</p>
<h3 class="fn"><a name="hostName" /><a href="qstring.html">QString</a> QSqlDatabase.hostName (<i>self</i>)</h3><p>Returns the connection's host name. It may be empty.</p>
<p>See also <a href="qsqldatabase.html#setHostName">setHostName</a>().</p>
<h3 class="fn"><a name="isDriverAvailable" />bool QSqlDatabase.isDriverAvailable (<a href="qstring.html">QString</a> <i>name</i>)</h3><p>Returns true if a driver called <i>name</i> is available; otherwise returns false.</p>
<p>See also <a href="qsqldatabase.html#drivers">drivers</a>().</p>
<h3 class="fn"><a name="isOpen" />bool QSqlDatabase.isOpen (<i>self</i>)</h3><p>Returns true if the database connection is currently open; otherwise returns false.</p>
<h3 class="fn"><a name="isOpenError" />bool QSqlDatabase.isOpenError (<i>self</i>)</h3><p>Returns true if there was an error opening the database connection; otherwise returns false. Error information can be retrieved using the <a href="qsqldatabase.html#lastError">lastError</a>() function.</p>
<h3 class="fn"><a name="isValid" />bool QSqlDatabase.isValid (<i>self</i>)</h3><p>Returns true if the <a href="qsqldatabase.html">QSqlDatabase</a> has a valid driver.</p>
<p>Example:</p>
<pre>
QSqlDatabase db;
qDebug() << db.isValid(); // Returns false
db = QSqlDatabase.database("sales");
qDebug() << db.isValid(); // Returns true if "sales" connection exists
QSqlDatabase.removeDatabase("sales");
qDebug() << db.isValid(); // Returns false
</pre>
<h3 class="fn"><a name="lastError" /><a href="qsqlerror.html">QSqlError</a> QSqlDatabase.lastError (<i>self</i>)</h3><p>Returns information about the last error that occurred on the database.</p>
<p>Failures that occur in conjunction with an individual query are reported by <a href="qsqlquery.html#lastError">QSqlQuery.lastError</a>().</p>
<p>See also <a href="qsqlerror.html">QSqlError</a> and <a href="qsqlquery.html#lastError">QSqlQuery.lastError</a>().</p>
<h3 class="fn"><a name="open" />bool QSqlDatabase.open (<i>self</i>)</h3><p>Opens the database connection using the current connection values. Returns true on success; otherwise returns false. Error information can be retrieved using <a href="qsqldatabase.html#lastError">lastError</a>().</p>
<p>See also <a href="qsqldatabase.html#lastError">lastError</a>(), <a href="qsqldatabase.html#setDatabaseName">setDatabaseName</a>(), <a href="qsqldatabase.html#setUserName">setUserName</a>(), <a href="qsqldatabase.html#setPassword">setPassword</a>(), <a href="qsqldatabase.html#setHostName">setHostName</a>(), <a href="qsqldatabase.html#setPort">setPort</a>(), and <a href="qsqldatabase.html#setConnectOptions">setConnectOptions</a>().</p>
<h3 class="fn"><a name="open-2" />bool QSqlDatabase.open (<i>self</i>, <a href="qstring.html">QString</a> <i>user</i>, <a href="qstring.html">QString</a> <i>password</i>)</h3><p>This is an overloaded member function, provided for convenience.</p>
<p>Opens the database connection using the given <i>user</i> name and <i>password</i>. Returns true on success; otherwise returns false. Error information can be retrieved using the <a href="qsqldatabase.html#lastError">lastError</a>() function.</p>
<p>This function does not store the password it is given. Instead, the password is passed directly to the driver for opening the connection and it is then discarded.</p>
<p>See also <a href="qsqldatabase.html#lastError">lastError</a>().</p>
<h3 class="fn"><a name="password" /><a href="qstring.html">QString</a> QSqlDatabase.password (<i>self</i>)</h3><p>Returns the connection's password. If the password was not set with <a href="qsqldatabase.html#setPassword">setPassword</a>(), and if the password was given in the <a href="qsqldatabase.html#open">open</a>() call, or if no password was used, an empty string is returned.</p>
<p>See also <a href="qsqldatabase.html#setPassword">setPassword</a>().</p>
<h3 class="fn"><a name="port" />int QSqlDatabase.port (<i>self</i>)</h3><p>Returns the connection's port number. The value is undefined if the port number has not been set.</p>
<p>See also <a href="qsqldatabase.html#setPort">setPort</a>().</p>
<h3 class="fn"><a name="primaryIndex" /><a href="qsqlindex.html">QSqlIndex</a> QSqlDatabase.primaryIndex (<i>self</i>, <a href="qstring.html">QString</a> <i>tablename</i>)</h3><p>Returns the primary index for table <i>tablename</i>. If no primary index exists an empty <a href="qsqlindex.html">QSqlIndex</a> is returned.</p>
<p>See also <a href="qsqldatabase.html#tables">tables</a>() and <a href="qsqldatabase.html#record">record</a>().</p>
<h3 class="fn"><a name="record" /><a href="qsqlrecord.html">QSqlRecord</a> QSqlDatabase.record (<i>self</i>, <a href="qstring.html">QString</a> <i>tablename</i>)</h3><p>Returns a <a href="qsqlrecord.html">QSqlRecord</a> populated with the names of all the fields in the table (or view) called <i>tablename</i>. The order in which the fields appear in the record is undefined. If no such table (or view) exists, an empty record is returned.</p>
<h3 class="fn"><a name="registerSqlDriver" />QSqlDatabase.registerSqlDriver (<a href="qstring.html">QString</a> <i>name</i>, <a href="qsqldrivercreatorbase.html">QSqlDriverCreatorBase</a> <i>creator</i>)</h3><p>The <i>creator</i> argument has it's ownership transferred to Qt.</p><p>This function registers a new SQL driver called <i>name</i>, within the SQL framework. This is useful if you have a custom SQL driver and don't want to compile it as a plugin.</p>
<p>Example:</p>
<pre>
QSqlDatabase.registerSqlDriver("MYDRIVER",
new QSqlDriverCreator<MyDatabaseDriver>);
QSqlDatabase db = QSqlDatabase.addDatabase("MYDRIVER");
</pre>
<p><a href="qsqldatabase.html">QSqlDatabase</a> takes ownership of the <i>creator</i> pointer, so you mustn't delete it yourself.</p>
<p>See also <a href="qsqldatabase.html#drivers">drivers</a>().</p>
<h3 class="fn"><a name="removeDatabase" />QSqlDatabase.removeDatabase (<a href="qstring.html">QString</a> <i>connectionName</i>)</h3><p>Removes the database connection <i>connectionName</i> from the list of database connections.</p>
<p><b>Warning:</b> There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.</p>
<p>Example:</p>
<pre>
// 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
</pre>
<p>The correct way to do it:</p>
<pre>
{
QSqlDatabase db = QSqlDatabase.database("sales");
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
}
// Both "db" and "query" are destroyed because they are out of scope
QSqlDatabase.removeDatabase("sales"); // correct
</pre>
<p><b>Note:</b> This function is <a href="threads.html#thread-safe">thread-safe</a>.</p>
<p>See also <a href="qsqldatabase.html#database">database</a>().</p>
<h3 class="fn"><a name="rollback" />bool QSqlDatabase.rollback (<i>self</i>)</h3><p>Rolls a transaction back on the database if the driver supports transactions and a <a href="qsqldatabase.html#transaction">transaction</a>() has been started. Returns true if the operation succeeded; otherwise returns false.</p>
<p>See also <a href="qsqldriver.html#hasFeature">QSqlDriver.hasFeature</a>() and <a href="qsqldatabase.html#commit">commit</a>().</p>
<h3 class="fn"><a name="setConnectOptions" />QSqlDatabase.setConnectOptions (<i>self</i>, <a href="qstring.html">QString</a> <i>options</i> = QString())</h3><p>Sets database-specific <i>options</i>. This must be done before the connection is opened or it has no effect (or you can <a href="qsqldatabase.html#close">close</a>() the connection, call this function and <a href="qsqldatabase.html#open">open</a>() the connection again).</p>
<p>The format of the <i>options</i> string is a semicolon separated list of option names or option=value pairs. The options depend on the database client used:</p>
<table align="center" border="0" cellpadding="2" cellspacing="1">
<tr bgcolor="#A2C511" valign="top">
<th>ODBC</th>
<th>MySQL</th>
<th>PostgreSQL</th>
</tr>
<tr bgcolor="#F0F0F0" valign="top">
<td>
<ul>
<li>SQL_ATTR_ACCESS_MODE</li>
<li>SQL_ATTR_LOGIN_TIMEOUT</li>
<li>SQL_ATTR_CONNECTION_TIMEOUT</li>
<li>SQL_ATTR_CURRENT_CATALOG</li>
<li>SQL_ATTR_METADATA_ID</li>
<li>SQL_ATTR_PACKET_SIZE</li>
<li>SQL_ATTR_TRACEFILE</li>
<li>SQL_ATTR_TRACE</li>
<li>SQL_ATTR_CONNECTION_POOLING</li>
</ul>
</td>
<td>
<ul>
<li>CLIENT_COMPRESS</li>
<li>CLIENT_FOUND_ROWS</li>
<li>CLIENT_IGNORE_SPACE</li>
<li>CLIENT_SSL</li>
<li>CLIENT_ODBC</li>
<li>CLIENT_NO_SCHEMA</li>
<li>CLIENT_INTERACTIVE</li>
</ul>
</td>
<td>
<ul>
<li>connect_timeout</li>
<li>options</li>
<li>tty</li>
<li>requiressl</li>
<li>service</li>
</ul>
</td>
</tr>
<tr bgcolor="#A2C511" valign="top">
<th>DB2</th>
<th>OCI</th>
<th>TDS</th>
</tr>
<tr bgcolor="#E0E0E0" valign="top">
<td>
<ul>
<li>SQL_ATTR_ACCESS_MODE</li>
<li>SQL_ATTR_LOGIN_TIMEOUT</li>
</ul>
</td>
<td>
<ul>
<li>OCI_ATTR_PREFETCH_ROWS</li>
<li>OCI_ATTR_PREFETCH_MEMORY</li>
</ul>
</td>
<td><i>none</i></td>
</tr>
<tr bgcolor="#A2C511" valign="top">
<th><a href="3rdparty.html#sqlite">SQLite</a></th>
</tr>
<tr bgcolor="#F0F0F0" valign="top">
<td>
<ul>
<li>QSQLITE_BUSY_TIMEOUT</li>
</ul>
</td>
</tr>
</table>
<br />
<br />
<p>Examples:</p>
<pre>
...
// MySQL connection
db.setConnectOptions("CLIENT_SSL;CLIENT_IGNORE_SPACE"); // 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
...
}
</pre>
<p>Refer to the client library documentation for more information about the different options.</p>
<p>See also <a href="qsqldatabase.html#connectOptions">connectOptions</a>().</p>
<h3 class="fn"><a name="setDatabaseName" />QSqlDatabase.setDatabaseName (<i>self</i>, <a href="qstring.html">QString</a> <i>name</i>)</h3><p>Sets the connection's name to <i>name</i>. This must be done before the connection is opened or it has no effect; (or you can <a href="qsqldatabase.html#close">close</a>() the connection, call this function and <a href="qsqldatabase.html#open">open</a>() the connection again). The name is database-specific.</p>
<p>For the QOCI (Oracle) driver, the database name is the TNS Service Name.</p>
<p>For the QODBC driver, the <i>name</i> can either be a DSN, a DSN filename (in which case the file must have a <tt>.dsn</tt> extension), or a connection string.</p>
<p>For example, Microsoft Access users can use the following connection string to open an <tt>.mdb</tt> file directly, instead of having to create a DSN entry in the ODBC manager:</p>
<pre>
...
db = QSqlDatabase.addDatabase("QODBC");
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb");
if (db.open()) {
// success!
}
...
</pre>
<p>There is no default value.</p>
<p>See also <a href="qsqldatabase.html#databaseName">databaseName</a>(), <a href="qsqldatabase.html#setUserName">setUserName</a>(), <a href="qsqldatabase.html#setPassword">setPassword</a>(), <a href="qsqldatabase.html#setHostName">setHostName</a>(), <a href="qsqldatabase.html#setPort">setPort</a>(), <a href="qsqldatabase.html#setConnectOptions">setConnectOptions</a>(), and <a href="qsqldatabase.html#open">open</a>().</p>
<h3 class="fn"><a name="setHostName" />QSqlDatabase.setHostName (<i>self</i>, <a href="qstring.html">QString</a> <i>host</i>)</h3><p>Sets the connection's host name to <i>host</i>. This must be done before the connection is opened or it has no effect (or you can <a href="qsqldatabase.html#close">close</a>() the connection, call this function and <a href="qsqldatabase.html#open">open</a>() the connection again).</p>
<p>There is no default value.</p>
<p>See also <a href="qsqldatabase.html#hostName">hostName</a>(), <a href="qsqldatabase.html#setUserName">setUserName</a>(), <a href="qsqldatabase.html#setPassword">setPassword</a>(), <a href="qsqldatabase.html#setDatabaseName">setDatabaseName</a>(), <a href="qsqldatabase.html#setPort">setPort</a>(), <a href="qsqldatabase.html#setConnectOptions">setConnectOptions</a>(), and <a href="qsqldatabase.html#open">open</a>().</p>
<h3 class="fn"><a name="setPassword" />QSqlDatabase.setPassword (<i>self</i>, <a href="qstring.html">QString</a> <i>password</i>)</h3><p>Sets the connection's password to <i>password</i>. This must be done before the connection is opened or it has no effect (or you can <a href="qsqldatabase.html#close">close</a>() the connection, call this function and <a href="qsqldatabase.html#open">open</a>() the connection again).</p>
<p>There is no default value.</p>
<p><b>Warning:</b> This function stores the password in plain text within Qt. Use the <a href="qsqldatabase.html#open">open</a>() call that takes a password as parameter to avoid this behavior.</p>
<p>See also <a href="qsqldatabase.html#password">password</a>(), <a href="qsqldatabase.html#setUserName">setUserName</a>(), <a href="qsqldatabase.html#setDatabaseName">setDatabaseName</a>(), <a href="qsqldatabase.html#setHostName">setHostName</a>(), <a href="qsqldatabase.html#setPort">setPort</a>(), <a href="qsqldatabase.html#setConnectOptions">setConnectOptions</a>(), and <a href="qsqldatabase.html#open">open</a>().</p>
<h3 class="fn"><a name="setPort" />QSqlDatabase.setPort (<i>self</i>, int <i>p</i>)</h3><p>Sets the connection's port number to <i>port</i>. This must be done before the connection is opened or it has no effect (or you can <a href="qsqldatabase.html#close">close</a>() the connection, call this function and <a href="qsqldatabase.html#open">open</a>() the connection again).</p>
<p>There is no default value.</p>
<p><a href="qsqldatabase.html#setDatabaseName">setDatabaseName</a>() <a href="qsqldatabase.html#setConnectOptions">setConnectOptions</a>() <a href="qsqldatabase.html#open">open</a>()</p>
<p>See also <a href="qsqldatabase.html#port">port</a>(), <a href="qsqldatabase.html#setUserName">setUserName</a>(), <a href="qsqldatabase.html#setPassword">setPassword</a>(), and <a href="qsqldatabase.html#setHostName">setHostName</a>().</p>
<h3 class="fn"><a name="setUserName" />QSqlDatabase.setUserName (<i>self</i>, <a href="qstring.html">QString</a> <i>name</i>)</h3><p>Sets the connection's user name to <i>name</i>. This must be done before the connection is opened or it has no effect (or you can <a href="qsqldatabase.html#close">close</a>() the connection, call this function and <a href="qsqldatabase.html#open">open</a>() the connection again).</p>
<p>There is no default value.</p>
<p><a href="qsqldatabase.html#setPort">setPort</a>() <a href="qsqldatabase.html#setConnectOptions">setConnectOptions</a>() <a href="qsqldatabase.html#open">open</a>()</p>
<p>See also <a href="qsqldatabase.html#userName">userName</a>(), <a href="qsqldatabase.html#setDatabaseName">setDatabaseName</a>(), <a href="qsqldatabase.html#setPassword">setPassword</a>(), and <a href="qsqldatabase.html#setHostName">setHostName</a>().</p>
<h3 class="fn"><a name="tables" /><a href="qstringlist.html">QStringList</a> QSqlDatabase.tables (<i>self</i>, <a href="qsql.html#TableType-enum">QSql.TableType</a> <i>type</i> = QSql.Tables)</h3><p>Returns a list of the database's tables, system tables and views, as specified by the parameter <i>type</i>.</p>
<p>See also <a href="qsqldatabase.html#primaryIndex">primaryIndex</a>() and <a href="qsqldatabase.html#record">record</a>().</p>
<h3 class="fn"><a name="transaction" />bool QSqlDatabase.transaction (<i>self</i>)</h3><p>Begins a transaction on the database if the driver supports transactions. Returns true if the operation succeeded; otherwise returns false.</p>
<p>See also <a href="qsqldriver.html#hasFeature">QSqlDriver.hasFeature</a>(), <a href="qsqldatabase.html#commit">commit</a>(), and <a href="qsqldatabase.html#rollback">rollback</a>().</p>
<h3 class="fn"><a name="userName" /><a href="qstring.html">QString</a> QSqlDatabase.userName (<i>self</i>)</h3><p>Returns the connection's user name; it may be empty.</p>
<p>See also <a href="qsqldatabase.html#setUserName">setUserName</a>().</p>
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td width="25%">PyQt 4.0.1 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.trolltech.com">Trolltech AS</a> 2006</td><td align="right" width="25%">Qt 4.1.4</td></tr></table></div></address></body></html>
|