<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- sql-programming.qdoc --> <head> <title>Qt 4.6: Connecting to Databases</title> <link rel="prev" href="sql-programming.html" /> <link rel="contents" href="sql-programming.html" /> <link rel="next" href="sql-sqlstatements.html" /> <link href="classic.css" rel="stylesheet" type="text/css" /> </head> <body> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></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="functions.html"><font color="#004faf">All Functions</font></a> · <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><p> [Previous: <a href="sql-programming.html">SQL Programming</a>] [<a href="sql-programming.html">SQL Programming</a>] [Next: <a href="sql-sqlstatements.html">Executing SQL Statements</a>] </p> <h1 class="title">Connecting to Databases<br /><span class="subtitle"></span> </h1> <p>To access a database with <a href="qsqlquery.html">QSqlQuery</a> or <a href="qsqlquerymodel.html">QSqlQueryModel</a>, create and open one or more database connections. Database connections are normally identified by connection name, <i>not</i> by database name. You can have multiple connections to the same database. <a href="qsqldatabase.html">QSqlDatabase</a> also supports the concept of a <i>default</i> connection, which is an unnamed connection. When calling <a href="qsqlquery.html">QSqlQuery</a> or <a href="qsqlquerymodel.html">QSqlQueryModel</a> member functions that take a connection name argument, if you don't pass a connection name, the default connection will be used. Creating a default connection is convenient when your application only requires one database connection.</p> <p>Note the difference between creating a connection and opening it. Creating a connection involves creating an instance of class <a href="qsqldatabase.html">QSqlDatabase</a>. The connection is not usable until it is opened. The following snippet shows how to create a <i>default</i> connection and then open it:</p> <pre> QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("bigblue"); db.setDatabaseName("flightdb"); db.setUserName("acarlson"); db.setPassword("1uTbSbAs"); bool ok = db.open();</pre> <p>The first line creates the connection object, and the last line opens it for use. In between, we initialize some connection information, including the <a href="qsqldatabase.html#setDatabaseName">database name</a>, the <a href="qsqldatabase.html#setHostName">host name</a>, the <a href="qsqldatabase.html#setUserName">user name</a>, and the <a href="qsqldatabase.html#setPassword">password</a>. In this case, we are connecting to the MySQL database <tt>flightdb</tt> on the host <tt>bigblue</tt>. The <tt>"QMYSQL"</tt> argument to <a href="qsqldatabase.html#addDatabase">addDatabase()</a> specifies the type of database driver to use for the connection. The set of database drivers included with Qt are shown in the table of <a href="sql-driver.html#supported-databases">supported database drivers</a>.</p> <p>The connection in the snippet will be the <i>default</i> connection, because we don't pass the second argument to <a href="qsqldatabase.html#addDatabase">addDatabase()</a>, which is the connection name. For example, here we establish two MySQL database connections named <tt>"first"</tt> and <tt>"second"</tt>:</p> <pre> QSqlDatabase firstDB = QSqlDatabase::addDatabase("QMYSQL", "first"); QSqlDatabase secondDB = QSqlDatabase::addDatabase("QMYSQL", "second");</pre> <p>After these connections have been initialized, <a href="qsqldatabase.html#open">open()</a> for each one to establish the live connections. If the <a href="qsqldatabase.html#open">open()</a> fails, it returns false. In that case, call <a href="qsqldatabase.html#lastError">QSqlDatabase::lastError</a>() to get error information.</p> <p>Once a connection is established, we can call the static function <a href="qsqldatabase.html#database">QSqlDatabase::database</a>() from anywhere with a connection name to get a pointer to that database connection. If we don't pass a connection name, it will return the default connection. For example:</p> <pre> QSqlDatabase defaultDB = QSqlDatabase::database(); QSqlDatabase firstDB = QSqlDatabase::database("first"); QSqlDatabase secondDB = QSqlDatabase::database("second");</pre> <p>To remove a database connection, first close the database using <a href="qsqldatabase.html#close">QSqlDatabase::close</a>(), then remove it using the static method <a href="qsqldatabase.html#removeDatabase">QSqlDatabase::removeDatabase</a>().</p> <p> [Previous: <a href="sql-programming.html">SQL Programming</a>] [<a href="sql-programming.html">SQL Programming</a>] [Next: <a href="sql-sqlstatements.html">Executing SQL Statements</a>] </p> <p /><address><hr /><div align="center"> <table width="100%" cellspacing="0" border="0"><tr class="address"> <td width="40%" align="left">Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies)</td> <td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td> <td width="40%" align="right"><div align="right">Qt 4.6.3</div></td> </tr></table></div></address></body> </html>