Qyoto  4.0.5
Qyoto is a C# language binding for Qt
 All Classes Namespaces Functions Variables Typedefs Enumerations Properties
QtSql.QSqlDatabase Class Reference

The QSqlDatabase class represents a connection to a database. More...

Inheritance diagram for QtSql.QSqlDatabase:
Collaboration diagram for QtSql.QSqlDatabase:

Public Member Functions

 QSqlDatabase ()
 
 
 QSqlDatabase (QSqlDatabase other)
 
 
virtual void CreateProxy ()
 
new void Close ()
 
 
new bool Commit ()
 
 
new string ConnectionName ()
 
 
new QSqlDriver Driver ()
 
 
new string DriverName ()
 
 
new QSqlQuery Exec (string query="")
 
 
new bool IsOpen ()
 
 
new bool IsOpenError ()
 
 
new bool IsValid ()
 
 
new QSqlError LastError ()
 
 
new bool Open ()
 
 
new bool Open (string user, string password)
 
 
new QSqlIndex PrimaryIndex (string tablename)
 
 
new QSqlRecord Record (string tablename)
 
 
new bool Rollback ()
 
 
new
System.Collections.Generic.List
< string > 
Tables (QSql.TableType type=QSql.TableType.Tables)
 
 
new bool Transaction ()
 
 
new void Dispose ()
 

Static Public Member Functions

static QSqlDatabase AddDatabase (QSqlDriver driver)
 
 
static QSqlDatabase AddDatabase (QSqlDriver driver, string connectionName)
 
 
static QSqlDatabase AddDatabase (string type)
 
 
static QSqlDatabase AddDatabase (string type, string connectionName)
 
 
static QSqlDatabase CloneDatabase (QSqlDatabase other, string connectionName)
 
 
static
System.Collections.Generic.List
< string > 
ConnectionNames ()
 
 
static bool Contains ()
 
 
static bool Contains (string connectionName)
 
 
static QSqlDatabase Database ()
 
 
static QSqlDatabase Database (string connectionName, bool open=true)
 
 
static
System.Collections.Generic.List
< string > 
Drivers ()
 
 
static bool IsDriverAvailable (string name)
 
 
static void RegisterSqlDriver (string name, QSqlDriverCreatorBase creator)
 
 
static void RemoveDatabase (string connectionName)
 
 

Protected Member Functions

 QSqlDatabase (System.Type dummy)
 
 QSqlDatabase (QSqlDriver driver)
 
 
 QSqlDatabase (string type)
 
 

Protected Attributes

SmokeInvocation interceptor
 

Properties

new string ConnectOptions [get, set]
 
 
new string DatabaseName [get, set]
 
 
new string HostName [get, set]
 
 
new QSql.NumericalPrecisionPolicy NumericalPrecisionPolicy [get, set]
 
 
new string Password [get, set]
 
 
new int Port [get, set]
 
 
new string UserName [get, set]
 
 
virtual System.IntPtr SmokeObject [get, set]
 
static new string DefaultConnection [get, set]
 

Detailed Description

The QSqlDatabase class represents a connection to a database.

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 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 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 PostgreSQL database:

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");

db.setHostName("acidalia");

db.setDatabaseName("customdb");

db.setUserName("mojito");

db.setPassword("J0a1m8");

bool ok = db.open();

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 addDatabase(). Subsequently, you can get the default connection by calling database() without the connection name argument:

QSqlDatabase db = QSqlDatabase::database();

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 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, and Threads and the SQL Module.

Constructor & Destructor Documentation

QtSql.QSqlDatabase.QSqlDatabase ( System.Type  dummy)
protected
QtSql.QSqlDatabase.QSqlDatabase ( )

Creates an empty, invalid QSqlDatabase object. Use addDatabase(), removeDatabase(), and database() to get valid QSqlDatabase objects.

QtSql.QSqlDatabase.QSqlDatabase ( QSqlDatabase  other)

Creates a copy of other.

QtSql.QSqlDatabase.QSqlDatabase ( QSqlDriver  driver)
protected

This is an overloaded function.

Creates a database connection using the given driver.

QtSql.QSqlDatabase.QSqlDatabase ( string  type)
protected

This is an overloaded function.

Creates a QSqlDatabase connection that uses the driver referred to by type. If the type is not recognized, the database connection will have no functionality.

The currently available driver types are:

Driver TypeDescription

QDB2 IBM DB2

QIBASE Borland InterBase Driver

QMYSQL MySQL Driver

QOCI Oracle Call Interface Driver

QODBC ODBC Driver (includes Microsoft SQL Server)

QPSQL PostgreSQL Driver

QSQLITE SQLite version 3 or above

QSQLITE2 SQLite version 2

QTDS Sybase Adaptive Server

Additional third party drivers, including your own custom drivers, can be loaded dynamically.

See also SQL Database Drivers, registerSqlDriver(), and drivers().

Member Function Documentation

static QSqlDatabase QtSql.QSqlDatabase.AddDatabase ( QSqlDriver  driver)
static

Adds a database to the list of database connections using the driver type and the connection name connectionName. If there already exists a database connection called connectionName, that connection is removed.

The database connection is referred to by connectionName. The newly added database connection is returned.

If type is not available or could not be loaded, isValid() returns false.

If connectionName is not specified, the new connection becomes the default connection for the application, and subsequent calls to database() without the connection name argument will return the default connection. If a connectionName is provided here, use database(connectionName) to retrieve the connection.

Warning: 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 connectionName, the default connection will be the one replaced.

Before using the connection, it must be initialized. e.g., call some or all of setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions(), and, finally, open().

Note: This function is thread-safe.

See also database(), removeDatabase(), and Threads and the SQL Module.

static QSqlDatabase QtSql.QSqlDatabase.AddDatabase ( QSqlDriver  driver,
string  connectionName 
)
static

Adds a database to the list of database connections using the driver type and the connection name connectionName. If there already exists a database connection called connectionName, that connection is removed.

The database connection is referred to by connectionName. The newly added database connection is returned.

If type is not available or could not be loaded, isValid() returns false.

If connectionName is not specified, the new connection becomes the default connection for the application, and subsequent calls to database() without the connection name argument will return the default connection. If a connectionName is provided here, use database(connectionName) to retrieve the connection.

Warning: 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 connectionName, the default connection will be the one replaced.

Before using the connection, it must be initialized. e.g., call some or all of setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions(), and, finally, open().

Note: This function is thread-safe.

See also database(), removeDatabase(), and Threads and the SQL Module.

static QSqlDatabase QtSql.QSqlDatabase.AddDatabase ( string  type)
static

Adds a database to the list of database connections using the driver type and the connection name connectionName. If there already exists a database connection called connectionName, that connection is removed.

The database connection is referred to by connectionName. The newly added database connection is returned.

If type is not available or could not be loaded, isValid() returns false.

If connectionName is not specified, the new connection becomes the default connection for the application, and subsequent calls to database() without the connection name argument will return the default connection. If a connectionName is provided here, use database(connectionName) to retrieve the connection.

Warning: 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 connectionName, the default connection will be the one replaced.

Before using the connection, it must be initialized. e.g., call some or all of setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions(), and, finally, open().

Note: This function is thread-safe.

See also database(), removeDatabase(), and Threads and the SQL Module.

static QSqlDatabase QtSql.QSqlDatabase.AddDatabase ( string  type,
string  connectionName 
)
static

Adds a database to the list of database connections using the driver type and the connection name connectionName. If there already exists a database connection called connectionName, that connection is removed.

The database connection is referred to by connectionName. The newly added database connection is returned.

If type is not available or could not be loaded, isValid() returns false.

If connectionName is not specified, the new connection becomes the default connection for the application, and subsequent calls to database() without the connection name argument will return the default connection. If a connectionName is provided here, use database(connectionName) to retrieve the connection.

Warning: 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 connectionName, the default connection will be the one replaced.

Before using the connection, it must be initialized. e.g., call some or all of setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions(), and, finally, open().

Note: This function is thread-safe.

See also database(), removeDatabase(), and Threads and the SQL Module.

static QSqlDatabase QtSql.QSqlDatabase.CloneDatabase ( QSqlDatabase  other,
string  connectionName 
)
static

Clones the database connection other and and stores it as connectionName. All the settings from the original database, e.g. databaseName(), hostName(), etc., are copied across. Does nothing if other is an invalid database. Returns the newly created database connection.

Note: The new connection has not been opened. Before using the new connection, you must call open().

new void QtSql.QSqlDatabase.Close ( )

Closes the database connection, freeing any resources acquired, and invalidating any existing QSqlQuery objects that are used with the database.

This will also affect copies of this QSqlDatabase object.

See also removeDatabase().

new bool QtSql.QSqlDatabase.Commit ( )

Commits a transaction to the database if the driver supports transactions and a transaction() has been started. Returns true if the operation succeeded. Otherwise it returns false.

Note: For some databases, the commit will fail and return false if there is an active query using the database for a SELECT. Make the query inactive before doing the commit.

Call lastError() to get information about errors.

See also QSqlQuery::isActive(), QSqlDriver::hasFeature(), and rollback().

new string QtSql.QSqlDatabase.ConnectionName ( )

Returns the connection name, which may be empty. Note: The connection name is not the database name.

This function was introduced in Qt 4.4.

See also addDatabase().

static System.Collections.Generic.List<string> QtSql.QSqlDatabase.ConnectionNames ( )
static

Returns a list containing the names of all connections.

Note: This function is thread-safe.

See also contains(), database(), and Threads and the SQL Module.

static bool QtSql.QSqlDatabase.Contains ( )
static

Returns true if the list of database connections contains connectionName; otherwise returns false.

Note: This function is thread-safe.

See also connectionNames(), database(), and Threads and the SQL Module.

static bool QtSql.QSqlDatabase.Contains ( string  connectionName)
static

Returns true if the list of database connections contains connectionName; otherwise returns false.

Note: This function is thread-safe.

See also connectionNames(), database(), and Threads and the SQL Module.

virtual void QtSql.QSqlDatabase.CreateProxy ( )
virtual
static QSqlDatabase QtSql.QSqlDatabase.Database ( )
static

Returns the database connection called connectionName. The database connection must have been previously added with addDatabase(). If open is true (the default) and the database connection is not already open it is opened now. If no connectionName is specified the default connection is used. If connectionName does not exist in the list of databases, an invalid connection is returned.

Note: This function is thread-safe.

See also isOpen() and Threads and the SQL Module.

static QSqlDatabase QtSql.QSqlDatabase.Database ( string  connectionName,
bool  open = true 
)
static

Returns the database connection called connectionName. The database connection must have been previously added with addDatabase(). If open is true (the default) and the database connection is not already open it is opened now. If no connectionName is specified the default connection is used. If connectionName does not exist in the list of databases, an invalid connection is returned.

Note: This function is thread-safe.

See also isOpen() and Threads and the SQL Module.

new void QtSql.QSqlDatabase.Dispose ( )
new QSqlDriver QtSql.QSqlDatabase.Driver ( )

Returns the database driver used to access the database connection.

See also addDatabase() and drivers().

new string QtSql.QSqlDatabase.DriverName ( )

Returns the connection's driver name.

See also addDatabase() and driver().

static System.Collections.Generic.List<string> QtSql.QSqlDatabase.Drivers ( )
static

Returns a list of all the available database drivers.

See also registerSqlDriver().

new QSqlQuery QtSql.QSqlDatabase.Exec ( string  query = "")

Executes a SQL statement on the database and returns a QSqlQuery object. Use lastError() to retrieve error information. If query is empty, an empty, invalid query is returned and lastError() is not affected.

See also QSqlQuery and lastError().

static bool QtSql.QSqlDatabase.IsDriverAvailable ( string  name)
static

Returns true if a driver called name is available; otherwise returns false.

See also drivers().

new bool QtSql.QSqlDatabase.IsOpen ( )

Returns true if the database connection is currently open; otherwise returns false.

new bool QtSql.QSqlDatabase.IsOpenError ( )

Returns true if there was an error opening the database connection; otherwise returns false. Error information can be retrieved using the lastError() function.

new bool QtSql.QSqlDatabase.IsValid ( )

Returns true if the QSqlDatabase has a valid driver.

Example:

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

new QSqlError QtSql.QSqlDatabase.LastError ( )

Returns information about the last error that occurred on the database.

Failures that occur in conjunction with an individual query are reported by QSqlQuery::lastError().

See also QSqlError and QSqlQuery::lastError().

new bool QtSql.QSqlDatabase.Open ( )

Opens the database connection using the current connection values. Returns true on success; otherwise returns false. Error information can be retrieved using lastError().

See also lastError(), setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions().

new bool QtSql.QSqlDatabase.Open ( string  user,
string  password 
)

This is an overloaded function.

Opens the database connection using the given user name and password. Returns true on success; otherwise returns false. Error information can be retrieved using the lastError() function.

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.

See also lastError().

new QSqlIndex QtSql.QSqlDatabase.PrimaryIndex ( string  tablename)

Returns the primary index for table tablename. If no primary index exists an empty QSqlIndex is returned.

See also tables() and record().

new QSqlRecord QtSql.QSqlDatabase.Record ( string  tablename)

Returns a QSqlRecord populated with the names of all the fields in the table (or view) called tablename. The order in which the fields appear in the record is undefined. If no such table (or view) exists, an empty record is returned.

static void QtSql.QSqlDatabase.RegisterSqlDriver ( string  name,
QSqlDriverCreatorBase  creator 
)
static

This function registers a new SQL driver called name, within the SQL framework. This is useful if you have a custom SQL driver and don't want to compile it as a plugin.

Example:

QSqlDatabase::registerSqlDriver("MYDRIVER",

new QSqlDriverCreator<MyDatabaseDriver>);

QSqlDatabase db = QSqlDatabase::addDatabase("MYDRIVER");

QSqlDatabase takes ownership of the creator pointer, so you mustn't delete it yourself.

See also drivers().

static void QtSql.QSqlDatabase.RemoveDatabase ( string  connectionName)
static

Removes the database connection connectionName from the list of database connections.

Warning: There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.

Example:

// 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

The correct way to do it:

{

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

To remove the default connection, which may have been created with a call to addDatabase() not specifying a connection name, you can retrieve the default connection name by calling connectionName() on the database returned by database(). Note that if a default database hasn't been created an invalid database will be returned.

Note: This function is thread-safe.

See also database(), connectionName(), and Threads and the SQL Module.

new bool QtSql.QSqlDatabase.Rollback ( )

Rolls back a transaction on the database, if the driver supports transactions and a transaction() has been started. Returns true if the operation succeeded. Otherwise it returns false.

Note: For some databases, the rollback will fail and return false if there is an active query using the database for a SELECT. Make the query inactive before doing the rollback.

Call lastError() to get information about errors.

See also QSqlQuery::isActive(), QSqlDriver::hasFeature(), and commit().

new System.Collections.Generic.List<string> QtSql.QSqlDatabase.Tables ( QSql.TableType  type = QSql.TableType.Tables)

Returns a list of the database's tables, system tables and views, as specified by the parameter type.

See also primaryIndex() and record().

new bool QtSql.QSqlDatabase.Transaction ( )

Begins a transaction on the database if the driver supports transactions. Returns true if the operation succeeded. Otherwise it returns false.

See also QSqlDriver::hasFeature(), commit(), and rollback().

Member Data Documentation

SmokeInvocation QtSql.QSqlDatabase.interceptor
protected

Property Documentation

new string QtSql.QSqlDatabase.ConnectOptions
getset

Returns the connection options string used for this connection. The string may be empty.

Sets database-specific options. This must be done before the connection is opened or it has no effect (or you can close() the connection, call this function and open() the connection again).

The format of the options string is a semicolon separated list of option names or option=value pairs. The options depend on the database client used:

ODBCMySQLPostgreSQL

SQL_ATTR_ACCESS_MODE

SQL_ATTR_LOGIN_TIMEOUT

SQL_ATTR_CONNECTION_TIMEOUT

SQL_ATTR_CURRENT_CATALOG

SQL_ATTR_METADATA_ID

SQL_ATTR_PACKET_SIZE

SQL_ATTR_TRACEFILE

SQL_ATTR_TRACE

SQL_ATTR_CONNECTION_POOLING

SQL_ATTR_ODBC_VERSION

new string QtSql.QSqlDatabase.DatabaseName
getset

Returns the connection's database name, which may be empty. Note: The database name is not the connection name.

Sets the connection's database name to name. To have effect, the database name must be set before the connection is opened. Alternatively, you can close() the connection, set the database name, and call open() again. Note: The database name is not the connection name. The connection name must be passed to addDatabase() at connection object create time.

For the QOCI (Oracle) driver, the database name is the TNS Service Name.

For the QODBC driver, the name can either be a DSN, a DSN filename (in which case the file must have a .dsn extension), or a connection string.

For example, Microsoft Access users can use the following connection string to open an .mdb file directly, instead of having to create a DSN entry in the ODBC manager:

...

db = QSqlDatabase::addDatabase("QODBC");

db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb");

if (db.open()) {

// success!

}

...

There is no default value.

new string QtSql.QSqlDatabase.DefaultConnection
staticgetset
new string QtSql.QSqlDatabase.HostName
getset

Returns the connection's host name; it may be empty.

Sets the connection's host name to host. To have effect, the host name must be set before the connection is opened. Alternatively, you can close() the connection, set the host name, and call open() again.

There is no default value.

new QSql.NumericalPrecisionPolicy QtSql.QSqlDatabase.NumericalPrecisionPolicy
getset

Returns the current default precision policy for the database connection.

This function was introduced in Qt 4.6.

Sets the default numerical precision policy used by queries created on this database connection to precisionPolicy.

Note: Drivers that don't support fetching numerical values with low precision will ignore the precision policy. You can use QSqlDriver::hasFeature() to find out whether a driver supports this feature.

Note: Setting the default precision policy to precisionPolicy doesn't affect any currently active queries.

This function was introduced in Qt 4.6.

new string QtSql.QSqlDatabase.Password
getset

Returns the connection's password. If the password was not set with setPassword(), and if the password was given in the open() call, or if no password was used, an empty string is returned.

Sets the connection's password to password. To have effect, the password must be set before the connection is opened. Alternatively, you can close() the connection, set the password, and call open() again.

There is no default value.

Warning: This function stores the password in plain text within Qt. Use the open() call that takes a password as parameter to avoid this behavior.

new int QtSql.QSqlDatabase.Port
getset

Returns the connection's port number. The value is undefined if the port number has not been set.

Sets the connection's port number to port. To have effect, the port number must be set before the connection is opened. Alternatively, you can close() the connection, set the port number, and call open() again..

There is no default value.

virtual System.IntPtr QtSql.QSqlDatabase.SmokeObject
getset
new string QtSql.QSqlDatabase.UserName
getset

Returns the connection's user name; it may be empty.

Sets the connection's user name to name. To have effect, the user name must be set before the connection is opened. Alternatively, you can close() the connection, set the user name, and call open() again.

There is no default value.