Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QSqlDatabase Class Reference

List of all members.


Detailed Description

The QSqlDatabase class represents a connection to a database.

sql

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 {SQL Database Drivers::Supported Databases} {supported database drivers}, which are derived from QSqlDriver. Alternatively, you can subclass your own database driver from QSqlDriver. See {How to Write Your Own Database Driver} for more information.

Create a connection (i.e., an instance of QSqlDatabase) by calling one of the static addDatabase() functions, where you specify {SQL Database Drivers::Supported Databases} {the driver or type of driver} to use (i.e., what kind of database will you access?) and a connection name. A connection is known by its own name, {not} by the name of the database it connects to. You can have multiple connections to one database. QSqlDatabase also supports the concept of a {default} connection, which is the unnamed connection. To create the default connection, don't pass the connection name argument when you call addDatabase(). 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 MySQL database:

doc/src/snippets/sqldatabase/sqldatabase.cpp 0

Once the QSqlDatabase object has been created, set the connection parameters with setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions(). Then call open() to activate the physical connection to the database. The connection is not usable until you open it.

The connection defined above will be the {default} connection, because we didn't give a connection name to {QSqlDatabase::} {addDatabase()}. Subsequently, you can get the default connection by calling database() without the connection name argument:

doc/src/snippets/sqldatabase/sqldatabase.cpp 1

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 cloneDatabase() to create an independent database connection based on an existing one.

If you create multiple database connections, specify a unique connection name for each one, when you call addDatabase(). Use database() with a connection name to get that connection. Use removeDatabase() 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 contains() to see if a given connection name is in the list of connections.

Once a connection is established, you can call tables() to get the list of tables in the database, call primaryIndex() to get a table's primary index, and call record() to get meta-information about a table's fields (e.g., field names).

Note:
QSqlDatabase::exec() is deprecated. Use QSqlQuery::exec() instead.
If the driver supports transactions, use transaction() to start a transaction, and commit() or rollback() to complete it. Use {QSqlDriver::} {hasFeature()} to ask if the driver supports transactions.
Note:
When using transactions, you must start the transaction before you create your query.
If an error occurrs, lastError() will return information about it.

Get the names of the available SQL drivers with drivers(). Check for the presence of a particular driver with isDriverAvailable(). If you have created your own custom driver, you must register it with registerSqlDriver().

See also:
QSqlDriver, QSqlQuery, {QtSql Module}, {Threads and the SQL Module}

Definition at line 74 of file qsqldatabase.h.


Public Member Functions

void close ()
bool commit ()
QString connectionName () const
QString connectOptions () const
QString databaseName () const
QSqlDriverdriver () const
QString driverName () const
QSqlQuery exec (const QString &query=QString()) const
QString hostName () const
bool isOpen () const
bool isOpenError () const
bool isValid () const
QSqlError lastError () const
bool open (const QString &user, const QString &password)
bool open ()
QSqlDatabaseoperator= (const QSqlDatabase &other)
QString password () const
int port () const
QSqlIndex primaryIndex (const QString &tablename) const
 QSqlDatabase (const QSqlDatabase &other)
 QSqlDatabase ()
QSqlRecord record (const QString &tablename) const
bool rollback ()
void setConnectOptions (const QString &options=QString())
void setDatabaseName (const QString &name)
void setHostName (const QString &host)
void setPassword (const QString &password)
void setPort (int p)
void setUserName (const QString &name)
QStringList tables (QSql::TableType type=QSql::Tables) const
bool transaction ()
QString userName () const
 ~QSqlDatabase ()

Static Public Member Functions

static QSqlDatabase addDatabase (QSqlDriver *driver, const QString &connectionName=QLatin1String(defaultConnection))
static QSqlDatabase addDatabase (const QString &type, const QString &connectionName=QLatin1String(defaultConnection))
static QSqlDatabase cloneDatabase (const QSqlDatabase &other, const QString &connectionName)
static QStringList connectionNames ()
static bool contains (const QString &connectionName=QLatin1String(defaultConnection))
static QSqlDatabase database (const QString &connectionName=QLatin1String(defaultConnection), bool open=true)
static QStringList drivers ()
static bool isDriverAvailable (const QString &name)
static void registerSqlDriver (const QString &name, QSqlDriverCreatorBase *creator)
static void removeDatabase (const QString &connectionName)

Public Attributes

QT_STATIC_CONST char * defaultConnection

Protected Member Functions

 QSqlDatabase (QSqlDriver *driver)
 QSqlDatabase (const QString &type)

Private Attributes

QSqlDatabasePrivate * d

Friends

class QSqlDatabasePrivate

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index