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

QSqlDatabase Class Reference

List of all members.


Detailed Description

The QSqlDatabase class represents a connection to a database.

sql

The QSqlDatabase class provides an abstract interface for accessing database backends. It relies on database-specific {QSqlDriver}s to actually access and manipulate data.

The following code shows how to initialize a connection:

snippets/sqldatabase/sqldatabase.cpp QSqlDatabase_snippet QSqlDatabase db = db.open()

Once a QSqlDatabase object has been created you can set the connection parameters with setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions(). Once the parameters have been set up you can call open() to open the connection.

The connection defined above is a nameless connection. It is the default connection and can be accessed using database() later on:

QSqlDatabase db = QSqlDatabase db =

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

If you need multiple database connections simultaneously, specify an arbitrary name to addDatabase() and database(). Call removeDatabase() to remove connections. QSqlDatabase will output 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 see what tables the database offers with tables(), find the primary index for a table with primaryIndex(), get meta-information about a table's fields (e.g., their names) with record(), and execute a query with exec().

If transactions are supported, you can use transaction() to start a transaction, and then commit() or rollback() to complete it. You can find out whether transactions are supported using QSqlDriver::hasFeature(). When using transactions you must start the transaction before you create your query.

If an error occurred, it is given by lastError().

The names of the underlying SQL drivers are available from drivers(); you can check for a particular driver with isDriverAvailable(). If you have created your own custom driver, you can register it with registerSqlDriver().

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

Definition at line 73 of file qsqldatabase.h.


Public Member Functions

void close ()
bool commit ()
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