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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
|
<?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="index.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#connectionName">connectionName</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" />QSql.NumericalPrecisionPolicy <b><a href="qsqldatabase.html#numericalPrecisionPolicy">numericalPrecisionPolicy</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#setNumericalPrecisionPolicy">setNumericalPrecisionPolicy</a></b> (<i>self</i>, QSql.NumericalPrecisionPolicy <i>precisionPolicy</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 interface for accessing a
database through a connection. An instance of QSqlDatabase
represents the connection. The connection provides access to the
database via one of the <a href="sql-driver.html#supported-databases">supported database
drivers</a>, which are derived from <a href="qsqldriver.html">QSqlDriver</a>. Alternatively, you can subclass
your own database driver from <a href="qsqldriver.html">QSqlDriver</a>. See <a href="sql-driver.html#how-to-write-your-own-database-driver">How to
Write Your Own Database Driver</a> for more information.</p>
<p>Create a connection (i.e., an instance of QSqlDatabase) by
calling one of the static <a href="qsqldatabase.html#addDatabase">addDatabase</a>() functions, where
you specify <a href="sql-driver.html#supported-databases">the
driver or type of driver</a> to use (i.e., what kind of database
will you access?) and a connection name. A connection is known by
its own name, <i>not</i> by the name of the database it connects
to. You can have multiple connections to one database. QSqlDatabase
also supports the concept of a <i>default</i> connection, which is
the unnamed connection. To create the default connection, don't
pass the connection name argument when you call <a href="qsqldatabase.html#addDatabase">addDatabase</a>(). Subsequently,
when you call any static member function that takes the connection
name argument, if you don't pass the connection name argument, the
default connection is assumed. The following snippet shows how to
create and open a default connection to a PostgreSQL database:</p>
<pre class="cpp">
<span class="type">QSqlDatabase</span> db <span class="operator">=</span> <span class="type">QSqlDatabase</span><span class="operator">.</span>addDatabase(<span class="string">"QPSQL"</span>);
db<span class="operator">.</span>setHostName(<span class="string">"acidalia"</span>);
db<span class="operator">.</span>setDatabaseName(<span class="string">"customdb"</span>);
db<span class="operator">.</span>setUserName(<span class="string">"mojito"</span>);
db<span class="operator">.</span>setPassword(<span class="string">"J0a1m8"</span>);
<span class="type">bool</span> ok <span class="operator">=</span> db<span class="operator">.</span>open();
</pre>
<p>Once the QSqlDatabase object has been created, 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>(). Then
call <a href="qsqldatabase.html#open">open</a>() to activate the
physical connection to the database. The connection is not usable
until you open it.</p>
<p>The connection defined above will be the <i>default</i>
connection, because we didn't give a connection name to <a href="qsqldatabase.html#addDatabase">addDatabase()</a>. Subsequently,
you can get the default connection by calling <a href="qsqldatabase.html#database">database</a>() without the connection
name argument:</p>
<pre class="cpp">
<span class="type">QSqlDatabase</span> db <span class="operator">=</span> <span class="type">QSqlDatabase</span><span class="operator">.</span>database();
</pre>
<p>QSqlDatabase is a value class. Changes made to a database
connection via one instance of QSqlDatabase will affect other
instances of QSqlDatabase that represent the same connection. Use
<a href="qsqldatabase.html#cloneDatabase">cloneDatabase</a>() to
create an independent database connection based on an existing
one.</p>
<p>If you create multiple database connections, specify a unique
connection name for each one, when you call <a href="qsqldatabase.html#addDatabase">addDatabase</a>(). Use <a href="qsqldatabase.html#database">database</a>() with a connection name
to get that connection. Use <a href="qsqldatabase.html#removeDatabase">removeDatabase</a>() with a
connection name to remove a connection. QSqlDatabase outputs 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 call <a href="qsqldatabase.html#tables">tables</a>() to get the list of tables
in the database, call <a href="qsqldatabase.html#primaryIndex">primaryIndex</a>() to get a
table's primary index, and call <a href="qsqldatabase.html#record">record</a>() to get meta-information
about a table's fields (e.g., field names).</p>
<p><b>Note:</b> <a href="qsqldatabase.html#exec">QSqlDatabase.exec</a>() is deprecated.
Use <a href="qsqlquery.html#exec">QSqlQuery.exec</a>()
instead.</p>
<p>If the driver supports transactions, use <a href="qsqldatabase.html#transaction">transaction</a>() to start a
transaction, and <a href="qsqldatabase.html#commit">commit</a>() or
<a href="qsqldatabase.html#rollback">rollback</a>() to complete it.
Use <a href="qsqldriver.html#hasFeature">hasFeature()</a> to ask if
the driver supports transactions. <b>Note:</b> When using
transactions, you must start the transaction before you create your
query.</p>
<p>If an error occurrs, <a href="qsqldatabase.html#lastError">lastError</a>() will return
information about it.</p>
<p>Get the names of the available SQL drivers with <a href="qsqldatabase.html#drivers">drivers</a>(). Check for the presence
of a particular driver with <a href="qsqldatabase.html#isDriverAvailable">isDriverAvailable</a>(). If
you have created your own custom driver, you must register it with
<a href="qsqldatabase.html#registerSqlDriver">registerSqlDriver</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>, QString <i>type</i>)</h3><p>This is an overloaded function.</p>
<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 class="generic">
<thead>
<tr class="qt-style">
<th>Driver Type</th>
<th>Description</th>
</tr>
</thead>
<tr class="odd" valign="top">
<td>QDB2</td>
<td>IBM DB2</td>
</tr>
<tr class="even" valign="top">
<td>QIBASE</td>
<td>Borland InterBase Driver</td>
</tr>
<tr class="odd" valign="top">
<td>QMYSQL</td>
<td>MySQL Driver</td>
</tr>
<tr class="even" valign="top">
<td>QOCI</td>
<td>Oracle Call Interface Driver</td>
</tr>
<tr class="odd" valign="top">
<td>QODBC</td>
<td>ODBC Driver (includes Microsoft SQL Server)</td>
</tr>
<tr class="even" valign="top">
<td>QPSQL</td>
<td>PostgreSQL Driver</td>
</tr>
<tr class="odd" valign="top">
<td>QSQLITE</td>
<td>SQLite version 3 or above</td>
</tr>
<tr class="even" valign="top">
<td>QSQLITE2</td>
<td>SQLite version 2</td>
</tr>
<tr class="odd" valign="top">
<td>QTDS</td>
<td>Sybase Adaptive Server</td>
</tr>
</table>
<p>Additional third party drivers, including your own custom
drivers, can be loaded dynamically.</p>
<p><b>See also</b> <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>This is an overloaded function.</p>
<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 (QString <i>type</i>, QString <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>type</i> is not available or could not be loaded, <a href="qsqldatabase.html#isValid">isValid</a>() returns false.</p>
<p>If <i>connectionName</i> is not specified, the new connection
becomes the default connection for the application, and subsequent
calls to <a href="qsqldatabase.html#database">database</a>()
without the connection name argument will return the default
connection. If a <i>connectionName</i> is provided here, use
database(<i>connectionName</i>) to retrieve the connection.</p>
<p><b>Warning:</b> If you add a connection with the same name as an
existing connection, the new connection replaces the old one. If
you call this function more than once without specifying
<i>connectionName</i>, the default connection will be the one
replaced.</p>
<p>Before using the connection, it must be initialized. e.g., call
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,
finally, <a href="qsqldatabase.html#open">open</a>().</p>
<p><b>Note:</b> This function is <a href="threads-reentrancy.html#thread-safe">thread-safe</a>.</p>
<p><b>See also</b> <a href="qsqldatabase.html#database">database</a>(), <a href="qsqldatabase.html#removeDatabase">removeDatabase</a>(), and
<a href="threads-modules.html#threads-and-the-sql-module">Threads
and the SQL Module</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>, QString <i>connectionName</i> = QLatin1String(QSqlDatabase.defaultConnection))</h3><p>This overload is useful when you want to create a database
connection with a <a href="qsqldriver.html">driver</a> you
instantiated yourself. It might be your own database driver, or you
might just need to instantiate one of the Qt drivers yourself. If
you do this, it is recommended that you include the driver code in
your application. For example, you can create a PostgreSQL
connection with your own QPSQL driver like this:</p>
<pre class="cpp">
<span class="preprocessor">#include "qtdir/src/sql/drivers/psql/qsql_psql.cpp"</span>
PGconn <span class="operator">*</span>con <span class="operator">=</span> PQconnectdb(<span class="string">"host=server user=bart password=simpson dbname=springfield"</span>);
<span class="type">QPSQLDriver</span> <span class="operator">*</span>drv <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QPSQLDriver</span>(con);
<span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span> db <span class="operator">=</span> <span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span><span class="operator">.</span><a href="qsqldatabase.html#addDatabase">addDatabase</a>(drv); <span class="comment">// becomes the new default connection</span>
<span class="type"><a href="qsqlquery.html">QSqlQuery</a></span> query;
query<span class="operator">.</span><a href="qsqldatabase.html#exec">exec_</a>(<span class="string">"SELECT NAME, ID FROM STAFF"</span>);
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
</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><b>Note:</b> 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>
<p>Remember that you must link your application against the
database client library. Make sure the client library is in your
linker's search path, and add lines like these to your
<tt>.pro</tt> file:</p>
<pre class="cpp">
unix:LIBS <span class="operator">+</span><span class="operator">=</span> <span class="operator">-</span>lpq
win32:LIBS <span class="operator">+</span><span class="operator">=</span> libpqdll<span class="operator">.</span>lib
</pre>
<p>The method described works for all the supplied drivers. The
only difference will be in the driver constructor arguments. Here
is a table of the drivers included with Qt, their source code
files, and their constructor arguments:</p>
<table class="generic">
<thead>
<tr class="qt-style">
<th>Driver</th>
<th>Class name</th>
<th>Constructor arguments</th>
<th>File to include</th>
</tr>
</thead>
<tr class="odd" valign="top">
<td>QPSQL</td>
<td>QPSQLDriver</td>
<td>PGconn *connection</td>
<td><tt>qsql_psql.cpp</tt></td>
</tr>
<tr class="even" valign="top">
<td>QMYSQL</td>
<td>QMYSQLDriver</td>
<td>MYSQL *connection</td>
<td><tt>qsql_mysql.cpp</tt></td>
</tr>
<tr class="odd" valign="top">
<td>QOCI</td>
<td>QOCIDriver</td>
<td>OCIEnv *environment, OCISvcCtx *serviceContext</td>
<td><tt>qsql_oci.cpp</tt></td>
</tr>
<tr class="even" valign="top">
<td>QODBC</td>
<td>QODBCDriver</td>
<td>SQLHANDLE environment, SQLHANDLE connection</td>
<td><tt>qsql_odbc.cpp</tt></td>
</tr>
<tr class="odd" valign="top">
<td>QDB2</td>
<td>QDB2</td>
<td>SQLHANDLE environment, SQLHANDLE connection</td>
<td><tt>qsql_db2.cpp</tt></td>
</tr>
<tr class="even" 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 class="odd" valign="top">
<td>QSQLITE</td>
<td>QSQLiteDriver</td>
<td>sqlite *connection</td>
<td><tt>qsql_sqlite.cpp</tt></td>
</tr>
<tr class="even" valign="top">
<td>QIBASE</td>
<td>QIBaseDriver</td>
<td>isc_db_handle connection</td>
<td><tt>qsql_ibase.cpp</tt></td>
</tr>
</table>
<p>The host name (or service name) is needed when constructing the
QTDSDriver for creating new connections for internal queries. This
is to prevent blocking when several <a href="qsqlquery.html">QSqlQuery</a> objects are used simultaneously.</p>
<p><b>Warning:</b> Adding a database connection with the same
connection name as an existing connection, causes the existing
connection to be replaced by the new one.</p>
<p><b>Warning:</b> The SQL framework takes ownership of the
<i>driver</i>. It must not be deleted. To remove the connection,
use <a href="qsqldatabase.html#removeDatabase">removeDatabase</a>().</p>
<p><b>See also</b> <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>, QString <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><b>Note:</b> The new connection has not been opened. Before
using the new connection, you must call <a href="qsqldatabase.html#open">open</a>().</p>
<h3 class="fn"><a name="close" />QSqlDatabase.close (<i>self</i>)</h3><p>Closes the database connection, freeing any resources acquired,
and invalidating any existing <a href="qsqlquery.html">QSqlQuery</a> objects that are used with the
database.</p>
<p>This will also affect copies of this <a href="qsqldatabase.html">QSqlDatabase</a> object.</p>
<p><b>See also</b> <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 <tt>true</tt> if the operation succeeded. Otherwise it
returns <tt>false</tt>.</p>
<p><b>Note:</b> For some databases, the commit will fail and return
<tt>false</tt> if there is an <a href="qsqlquery.html#isActive">active query</a> using the database for a
<tt>SELECT</tt>. Make the query <a href="qsqlquery.html#isActive">inactive</a> before doing the commit.</p>
<p>Call <a href="qsqldatabase.html#lastError">lastError</a>() to
get information about errors.</p>
<p><b>See also</b> <a href="qsqlquery.html#isActive">QSqlQuery.isActive</a>(), <a href="qsqldriver.html#hasFeature">QSqlDriver.hasFeature</a>(), and
<a href="qsqldatabase.html#rollback">rollback</a>().</p>
<h3 class="fn"><a name="connectionName" />QString QSqlDatabase.connectionName (<i>self</i>)</h3><p>Returns the connection name, which may be empty. <b>Note:</b>
The connection name is not the <a href="qsqldatabase.html#databaseName">database name</a>.</p>
<p>This function was introduced in Qt 4.4.</p>
<p><b>See also</b> <a href="qsqldatabase.html#addDatabase">addDatabase</a>().</p>
<h3 class="fn"><a name="connectionNames" />QStringList QSqlDatabase.connectionNames ()</h3><p>Returns a list containing the names of all connections.</p>
<p><b>Note:</b> This function is <a href="threads-reentrancy.html#thread-safe">thread-safe</a>.</p>
<p><b>See also</b> <a href="qsqldatabase.html#contains">contains</a>(), <a href="qsqldatabase.html#database">database</a>(), and <a href="threads-modules.html#threads-and-the-sql-module">Threads and the
SQL Module</a>.</p>
<h3 class="fn"><a name="connectOptions" />QString QSqlDatabase.connectOptions (<i>self</i>)</h3><p>Returns the connection options string used for this connection.
The string may be empty.</p>
<p><b>See also</b> <a href="qsqldatabase.html#setConnectOptions">setConnectOptions</a>().</p>
<h3 class="fn"><a name="contains" />bool QSqlDatabase.contains (QString <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-reentrancy.html#thread-safe">thread-safe</a>.</p>
<p><b>See also</b> <a href="qsqldatabase.html#connectionNames">connectionNames</a>(), <a href="qsqldatabase.html#database">database</a>(), and <a href="threads-modules.html#threads-and-the-sql-module">Threads and the
SQL Module</a>.</p>
<h3 class="fn"><a name="database" /><a href="qsqldatabase.html">QSqlDatabase</a> QSqlDatabase.database (QString <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-reentrancy.html#thread-safe">thread-safe</a>.</p>
<p><b>See also</b> <a href="qsqldatabase.html#isOpen">isOpen</a>()
and <a href="threads-modules.html#threads-and-the-sql-module">Threads and the
SQL Module</a>.</p>
<h3 class="fn"><a name="databaseName" />QString QSqlDatabase.databaseName (<i>self</i>)</h3><p>Returns the connection's database name, which may be empty.
<b>Note:</b> The database name is not the connection name.</p>
<p><b>See also</b> <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><b>See also</b> <a href="qsqldatabase.html#addDatabase">addDatabase</a>() and <a href="qsqldatabase.html#drivers">drivers</a>().</p>
<h3 class="fn"><a name="driverName" />QString QSqlDatabase.driverName (<i>self</i>)</h3><p>Returns the connection's driver name.</p>
<p><b>See also</b> <a href="qsqldatabase.html#addDatabase">addDatabase</a>() and <a href="qsqldatabase.html#driver">driver</a>().</p>
<h3 class="fn"><a name="drivers" />QStringList QSqlDatabase.drivers ()</h3><p>Returns a list of all the available database drivers.</p>
<p><b>See also</b> <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>, QString <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><b>See also</b> <a href="qsqlquery.html">QSqlQuery</a> and
<a href="qsqldatabase.html#lastError">lastError</a>().</p>
<h3 class="fn"><a name="hostName" />QString QSqlDatabase.hostName (<i>self</i>)</h3><p>Returns the connection's host name; it may be empty.</p>
<p><b>See also</b> <a href="qsqldatabase.html#setHostName">setHostName</a>().</p>
<h3 class="fn"><a name="isDriverAvailable" />bool QSqlDatabase.isDriverAvailable (QString <i>name</i>)</h3><p>Returns true if a driver called <i>name</i> is available;
otherwise returns false.</p>
<p><b>See also</b> <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 class="cpp">
<span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span> db;
<a href="qtcore.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> db<span class="operator">.</span>isValid(); <span class="comment">// Returns false</span>
db <span class="operator">=</span> <span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span><span class="operator">.</span><a href="qsqldatabase.html#database">database</a>(<span class="string">"sales"</span>);
<a href="qtcore.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> db<span class="operator">.</span>isValid(); <span class="comment">// Returns true if "sales" connection exists</span>
<span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span><span class="operator">.</span><a href="qsqldatabase.html#removeDatabase">removeDatabase</a>(<span class="string">"sales"</span>);
<a href="qtcore.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> db<span class="operator">.</span>isValid(); <span class="comment">// Returns false</span>
</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><b>See also</b> <a href="qsqlerror.html">QSqlError</a> and
<a href="qsqlquery.html#lastError">QSqlQuery.lastError</a>().</p>
<h3 class="fn"><a name="numericalPrecisionPolicy" /><a href="qsql.html#NumericalPrecisionPolicy-enum">QSql.NumericalPrecisionPolicy</a> QSqlDatabase.numericalPrecisionPolicy (<i>self</i>)</h3><p>Returns the current default precision policy for the database
connection.</p>
<p>This function was introduced in Qt 4.6.</p>
<p><b>See also</b> <a href="qsql.html#NumericalPrecisionPolicy-enum">QSql.NumericalPrecisionPolicy</a>,
<a href="qsqldatabase.html#setNumericalPrecisionPolicy">setNumericalPrecisionPolicy</a>(),
<a href="qsqlquery.html#numericalPrecisionPolicy">QSqlQuery.numericalPrecisionPolicy</a>(),
and <a href="qsqlquery.html#setNumericalPrecisionPolicy">QSqlQuery.setNumericalPrecisionPolicy</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><b>See also</b> <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>, QString <i>user</i>, QString <i>password</i>)</h3><p>This is an overloaded function.</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><b>See also</b> <a href="qsqldatabase.html#lastError">lastError</a>().</p>
<h3 class="fn"><a name="password" />QString 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><b>See also</b> <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><b>See also</b> <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>, QString <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><b>See also</b> <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>, QString <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 (QString <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 class="cpp">
<span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span><span class="operator">.</span>registerSqlDriver(<span class="string">"MYDRIVER"</span><span class="operator">,</span>
<span class="keyword">new</span> <span class="type"><a href="qsqldrivercreator.html">QSqlDriverCreator</a></span><span class="operator"><</span>MyDatabaseDriver<span class="operator">></span>);
<span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span> db <span class="operator">=</span> <span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span><span class="operator">.</span><a href="qsqldatabase.html#addDatabase">addDatabase</a>(<span class="string">"MYDRIVER"</span>);
</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><b>See also</b> <a href="qsqldatabase.html#drivers">drivers</a>().</p>
<h3 class="fn"><a name="removeDatabase" />QSqlDatabase.removeDatabase (QString <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 class="cpp">
<span class="comment">// WRONG</span>
<span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span> db <span class="operator">=</span> <span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span><span class="operator">.</span><a href="qsqldatabase.html#database">database</a>(<span class="string">"sales"</span>);
<span class="type"><a href="qsqlquery.html">QSqlQuery</a></span> query(<span class="string">"SELECT NAME, DOB FROM EMPLOYEES"</span><span class="operator">,</span> db);
<span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span><span class="operator">.</span>removeDatabase(<span class="string">"sales"</span>); <span class="comment">// will output a warning</span>
<span class="comment">// "db" is now a dangling invalid database connection,</span>
<span class="comment">// "query" contains an invalid result set</span>
</pre>
<p>The correct way to do it:</p>
<pre class="cpp">
{
<span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span> db <span class="operator">=</span> <span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span><span class="operator">.</span>database(<span class="string">"sales"</span>);
<span class="type"><a href="qsqlquery.html">QSqlQuery</a></span> query(<span class="string">"SELECT NAME, DOB FROM EMPLOYEES"</span><span class="operator">,</span> db);
}
<span class="comment">// Both "db" and "query" are destroyed because they are out of scope</span>
<span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span><span class="operator">.</span>removeDatabase(<span class="string">"sales"</span>); <span class="comment">// correct</span>
</pre>
<p>To remove the default connection, which may have been created
with a call to <a href="qsqldatabase.html#addDatabase">addDatabase</a>() not specifying a
connection name, you can retrieve the default connection name by
calling <a href="qsqldatabase.html#connectionName">connectionName</a>() on the
database returned by <a href="qsqldatabase.html#database">database</a>(). Note that if a default
database hasn't been created an invalid database will be
returned.</p>
<p><b>Note:</b> This function is <a href="threads-reentrancy.html#thread-safe">thread-safe</a>.</p>
<p><b>See also</b> <a href="qsqldatabase.html#database">database</a>(), <a href="qsqldatabase.html#connectionName">connectionName</a>(), and
<a href="threads-modules.html#threads-and-the-sql-module">Threads
and the SQL Module</a>.</p>
<h3 class="fn"><a name="rollback" />bool QSqlDatabase.rollback (<i>self</i>)</h3><p>Rolls back a transaction on the database, if the driver supports
transactions and a <a href="qsqldatabase.html#transaction">transaction</a>() has been started.
Returns <tt>true</tt> if the operation succeeded. Otherwise it
returns <tt>false</tt>.</p>
<p><b>Note:</b> For some databases, the rollback will fail and
return <tt>false</tt> if there is an <a href="qsqlquery.html#isActive">active query</a> using the database for a
<tt>SELECT</tt>. Make the query <a href="qsqlquery.html#isActive">inactive</a> before doing the
rollback.</p>
<p>Call <a href="qsqldatabase.html#lastError">lastError</a>() to
get information about errors.</p>
<p><b>See also</b> <a href="qsqlquery.html#isActive">QSqlQuery.isActive</a>(), <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>, QString <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 class="generic">
<thead>
<tr class="qt-style">
<th>ODBC</th>
<th>MySQL</th>
<th>PostgreSQL</th>
</tr>
</thead>
<tr class="odd" 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>
<li>SQL_ATTR_ODBC_VERSION</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>
<li>UNIX_SOCKET</li>
<li>MYSQL_OPT_RECONNECT</li>
</ul>
</td>
<td>
<ul>
<li>connect_timeout</li>
<li>options</li>
<li>tty</li>
<li>requiressl</li>
<li>service</li>
</ul>
</td>
</tr>
<thead>
<tr class="qt-style">
<th>DB2</th>
<th>OCI</th>
<th>TDS</th>
</tr>
</thead>
<tr class="even" 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>
<thead>
<tr class="qt-style">
<th>SQLite</th>
<th>Interbase</th>
</tr>
</thead>
<tr class="odd" valign="top">
<td>
<ul>
<li>QSQLITE_BUSY_TIMEOUT</li>
<li>QSQLITE_OPEN_READONLY</li>
<li>QSQLITE_ENABLE_SHARED_CACHE</li>
</ul>
</td>
<td>
<ul>
<li>ISC_DPB_LC_CTYPE</li>
<li>ISC_DPB_SQL_ROLE_NAME</li>
</ul>
</td>
</tr>
</table>
<p>Examples:</p>
<pre class="cpp">
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
<span class="comment">// MySQL connection</span>
db<span class="operator">.</span>setConnectOptions(<span class="string">"CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1"</span>); <span class="comment">// use an SSL connection to the server</span>
<span class="keyword">if</span> (<span class="operator">!</span>db<span class="operator">.</span>open()) {
db<span class="operator">.</span>setConnectOptions(); <span class="comment">// clears the connect option string</span>
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
}
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
<span class="comment">// PostgreSQL connection</span>
db<span class="operator">.</span>setConnectOptions(<span class="string">"requiressl=1"</span>); <span class="comment">// enable PostgreSQL SSL connections</span>
<span class="keyword">if</span> (<span class="operator">!</span>db<span class="operator">.</span>open()) {
db<span class="operator">.</span>setConnectOptions(); <span class="comment">// clear options</span>
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
}
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
<span class="comment">// ODBC connection</span>
db<span class="operator">.</span>setConnectOptions(<span class="string">"SQL_ATTR_ACCESS_MODE=SQL_MODE_READ_ONLY;SQL_ATTR_TRACE=SQL_OPT_TRACE_ON"</span>); <span class="comment">// set ODBC options</span>
<span class="keyword">if</span> (<span class="operator">!</span>db<span class="operator">.</span>open()) {
db<span class="operator">.</span>setConnectOptions(); <span class="comment">// don't try to set this option</span>
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
}
</pre>
<p>Refer to the client library documentation for more information
about the different options.</p>
<p><b>See also</b> <a href="qsqldatabase.html#connectOptions">connectOptions</a>().</p>
<h3 class="fn"><a name="setDatabaseName" />QSqlDatabase.setDatabaseName (<i>self</i>, QString <i>name</i>)</h3><p>Sets the connection's database name to <i>name</i>. To have
effect, the database name must be set <i>before</i> the connection
is <a href="qsqldatabase.html#open">opened</a>. Alternatively, you
can <a href="qsqldatabase.html#close">close</a>() the connection,
set the database name, and call <a href="qsqldatabase.html#open">open</a>() again. <b>Note:</b> The
<i>database name</i> is not the <i>connection name</i>. The
connection name must be passed to <a href="qsqldatabase.html#addDatabase">addDatabase</a>() at connection
object create time.</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 class="cpp">
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
db <span class="operator">=</span> <span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span><span class="operator">.</span><a href="qsqldatabase.html#addDatabase">addDatabase</a>(<span class="string">"QODBC"</span>);
db<span class="operator">.</span>setDatabaseName(<span class="string">"DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb"</span>);
<span class="keyword">if</span> (db<span class="operator">.</span>open()) {
<span class="comment">// success!</span>
}
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
</pre>
<p>There is no default value.</p>
<p><b>See also</b> <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>, QString <i>host</i>)</h3><p>Sets the connection's host name to <i>host</i>. To have effect,
the host name must be set <i>before</i> the connection is <a href="qsqldatabase.html#open">opened</a>. Alternatively, you can
<a href="qsqldatabase.html#close">close</a>() the connection, set
the host name, and call <a href="qsqldatabase.html#open">open</a>()
again.</p>
<p>There is no default value.</p>
<p><b>See also</b> <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="setNumericalPrecisionPolicy" />QSqlDatabase.setNumericalPrecisionPolicy (<i>self</i>, <a href="qsql.html#NumericalPrecisionPolicy-enum">QSql.NumericalPrecisionPolicy</a> <i>precisionPolicy</i>)</h3><p>Sets the default numerical precision policy used by queries
created on this database connection to <i>precisionPolicy</i>.</p>
<p>Note: Drivers that don't support fetching numerical values with
low precision will ignore the precision policy. You can use
<a href="qsqldriver.html#hasFeature">QSqlDriver.hasFeature</a>()
to find out whether a driver supports this feature.</p>
<p>Note: Setting the default precision policy to
<i>precisionPolicy</i> doesn't affect any currently active
queries.</p>
<p>This function was introduced in Qt 4.6.</p>
<p><b>See also</b> <a href="qsql.html#NumericalPrecisionPolicy-enum">QSql.NumericalPrecisionPolicy</a>,
<a href="qsqldatabase.html#numericalPrecisionPolicy">numericalPrecisionPolicy</a>(),
<a href="qsqlquery.html#setNumericalPrecisionPolicy">QSqlQuery.setNumericalPrecisionPolicy</a>(),
and <a href="qsqlquery.html#numericalPrecisionPolicy">QSqlQuery.numericalPrecisionPolicy</a>().</p>
<h3 class="fn"><a name="setPassword" />QSqlDatabase.setPassword (<i>self</i>, QString <i>password</i>)</h3><p>Sets the connection's password to <i>password</i>. To have
effect, the password must be set <i>before</i> the connection is
<a href="qsqldatabase.html#open">opened</a>. Alternatively, you can
<a href="qsqldatabase.html#close">close</a>() the connection, set
the password, and call <a href="qsqldatabase.html#open">open</a>()
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><b>See also</b> <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>. To have
effect, the port number must be set <i>before</i> the connection is
<a href="qsqldatabase.html#open">opened</a>. Alternatively, you can
<a href="qsqldatabase.html#close">close</a>() the connection, set
the port number, and call <a href="qsqldatabase.html#open">open</a>() again..</p>
<p>There is no default value.</p>
<p><b>See also</b> <a href="qsqldatabase.html#port">port</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#setDatabaseName">setDatabaseName</a>(), <a href="qsqldatabase.html#setConnectOptions">setConnectOptions</a>(), and
<a href="qsqldatabase.html#open">open</a>().</p>
<h3 class="fn"><a name="setUserName" />QSqlDatabase.setUserName (<i>self</i>, QString <i>name</i>)</h3><p>Sets the connection's user name to <i>name</i>. To have effect,
the user name must be set <i>before</i> the connection is <a href="qsqldatabase.html#open">opened</a>. Alternatively, you can
<a href="qsqldatabase.html#close">close</a>() the connection, set
the user name, and call <a href="qsqldatabase.html#open">open</a>()
again.</p>
<p>There is no default value.</p>
<p><b>See also</b> <a href="qsqldatabase.html#userName">userName</a>(), <a href="qsqldatabase.html#setDatabaseName">setDatabaseName</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="tables" />QStringList 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><b>See also</b> <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 <tt>true</tt> if the operation succeeded.
Otherwise it returns <tt>false</tt>.</p>
<p><b>See also</b> <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" />QString QSqlDatabase.userName (<i>self</i>)</h3><p>Returns the connection's user name; it may be empty.</p>
<p><b>See also</b> <a href="qsqldatabase.html#setUserName">setUserName</a>().</p>
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.9.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt 4.8.2</td></tr></table></div></address></body></html>
|