Qt Jambi Home

com.trolltech.qt.sql
Class QSqlDatabase

java.lang.Object
  extended by com.trolltech.qt.QSignalEmitter
      extended by com.trolltech.qt.QtJambiObject
          extended by com.trolltech.qt.sql.QSqlDatabase
All Implemented Interfaces:
QtJambiInterface

public class QSqlDatabase
extends QtJambiObject

The QSqlDatabase class represents a connection to a database.

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

The following code shows how to initialize a connection:

        QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
        db.setHostName("acidalia");
        db.setDatabaseName("customdb");
        db.setUserName("mojito");
        db.setPassword("J0a1m8");
        bool ok = 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::database();

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

Nested Class Summary
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I>
 
Constructor Summary
  QSqlDatabase()
          Creates an empty, invalid QSqlDatabase object.
  QSqlDatabase(QSqlDatabase other)
          Creates a copy of other.
protected QSqlDatabase(QSqlDriver driver)
          Creates a database connection using the given driver.
protected QSqlDatabase(java.lang.String type)
          Creates a QSqlDatabase connection that uses the driver referred to by type.
 
Method Summary
static QSqlDatabase addDatabase(QSqlDriver driver)
          Equivalent to addDatabase(driver, QLatin1String( defaultConnection )).
static QSqlDatabase addDatabase(QSqlDriver driver, java.lang.String connectionName)
          This function is useful if you need to set up the database connection and instantiate the driver yourself.
static QSqlDatabase addDatabase(java.lang.String type)
          Equivalent to addDatabase(type, QLatin1String( defaultConnection )).
static QSqlDatabase addDatabase(java.lang.String type, java.lang.String connectionName)
          Adds a database to the list of database connections using the driver type and the connection name connectionName.
static QSqlDatabase cloneDatabase(QSqlDatabase other, java.lang.String connectionName)
          Clones the database connection other and and stores it as connectionName.
 void close()
          Closes the database connection, freeing any resources acquired, and invalidating any existing QSqlQuery objects that are used with the database.
 boolean commit()
          Commits a transaction to the database if the driver supports transactions and a transaction has been started.
static java.util.List<java.lang.String> connectionNames()
          Returns a list containing the names of all connections.
 java.lang.String connectOptions()
          Returns the connection options string used for this connection.
static boolean contains()
          Equivalent to contains(QLatin1String( defaultConnection )).
static boolean contains(java.lang.String connectionName)
          Returns true if the list of database connections contains connectionName; otherwise returns false.
static QSqlDatabase database()
          Equivalent to database(QLatin1String( defaultConnection ), true).
static QSqlDatabase database(java.lang.String connectionName)
          Equivalent to database(connectionName, true).
static QSqlDatabase database(java.lang.String connectionName, boolean open)
          Returns the database connection called connectionName.
 java.lang.String databaseName()
          Returns the connection's database name; it may be empty.
static java.lang.String defaultConnection()
          Returns this QSqlDatabase's default connection.
 QSqlDriver driver()
          Returns the database driver used to access the database connection.
 java.lang.String driverName()
          Returns the connection's driver name.
static java.util.List<java.lang.String> drivers()
          Returns a list of all the available database drivers.
 QSqlQuery exec()
          Equivalent to exec(QString()).
 QSqlQuery exec(java.lang.String query)
          Executes a SQL statement on the database and returns a QSqlQuery object.
static QSqlDatabase fromNativePointer(QNativePointer nativePointer)
          This function returns the QSqlDatabase instance pointed to by nativePointer
 java.lang.String hostName()
          Returns the connection's host name.
static boolean isDriverAvailable(java.lang.String name)
          Returns true if a driver called name is available; otherwise returns false.
 boolean isOpen()
          Returns true if the database connection is currently open; otherwise returns false.
 boolean isOpenError()
          Returns true if there was an error opening the database connection; otherwise returns false.
 boolean isValid()
          Returns true if the QSqlDatabase has a valid driver.
 QSqlError lastError()
          Returns information about the last error that occurred on the database.
static QNativePointer nativePointerArray(QSqlDatabase[] array)
          This function returns a QNativePointer that is pointing to the specified QSqlDatabase array.
 boolean open()
          Opens the database connection using the current connection values.
 boolean open(java.lang.String user, java.lang.String password)
          Opens the database connection using the given user name and password.
 java.lang.String password()
          Returns the connection's password.
 int port()
          Returns the connection's port number.
 QSqlIndex primaryIndex(java.lang.String tablename)
          Returns the primary index for table tablename.
 QSqlRecord record(java.lang.String tablename)
          Returns a QSqlRecord populated with the names of all the fields in the table (or view) called tablename.
static void registerSqlDriver(java.lang.String name, QSqlDriverCreatorBase creator)
          This function registers a new SQL driver called name, within the SQL framework.
static void removeDatabase(java.lang.String connectionName)
          Removes the database connection connectionName from the list of database connections.
 boolean rollback()
          Rolls a transaction back on the database if the driver supports transactions and a transaction has been started.
 void setConnectOptions()
          Equivalent to setConnectOptions(QString()).
 void setConnectOptions(java.lang.String options)
          Sets database-specific options.
 void setDatabaseName(java.lang.String name)
          Sets the connection's name to name.
 void setHostName(java.lang.String host)
          Sets the connection's host name to host.
 void setPassword(java.lang.String password)
          Sets the connection's password to password.
 void setPort(int p)
          Sets the connection's port number to p.
 void setUserName(java.lang.String name)
          Sets the connection's user name to name.
 java.util.List<java.lang.String> tables()
          This is an overloaded method provided for convenience.
 java.util.List<java.lang.String> tables(QSql.TableType type)
          Returns a list of the database's tables, system tables and views, as specified by the parameter type.
 boolean transaction()
          Begins a transaction on the database if the driver supports transactions.
 java.lang.String userName()
          Returns the connection's user name; it may be empty.
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Constructor Detail

QSqlDatabase

protected QSqlDatabase(QSqlDriver driver)

Creates a database connection using the given driver.


QSqlDatabase

public QSqlDatabase(QSqlDatabase other)

Creates a copy of other.


QSqlDatabase

protected QSqlDatabase(java.lang.String type)

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
QDB2IBM DB2
QIBASEBorland InterBase Driver
QMYSQLMySQL Driver
QOCIOracle Call Interface Driver
QODBCODBC Driver (includes Microsoft SQL Server)
QPSQLPostgreSQL Driver
QSQLITESQLite version 3 or above
QSQLITE2SQLite version 2
QTDSSybase Adaptive Server

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

See Also:
SQL Database Drivers, registerSqlDriver, drivers

QSqlDatabase

public QSqlDatabase()

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

Method Detail

close

public final void 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

commit

public final boolean 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 returns false.

Note that on some databases, this function will not work if there is an active QSqlQuery on the database. Use the lastError function to retrieve database-specific error data about the error that occurred.

See Also:
QSqlDriver::hasFeature, rollback

connectOptions

public final java.lang.String connectOptions()

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

See Also:
setConnectOptions

databaseName

public final java.lang.String databaseName()

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

See Also:
setDatabaseName

driver

public final QSqlDriver driver()

Returns the database driver used to access the database connection.

See Also:
addDatabase, drivers

driverName

public final java.lang.String driverName()

Returns the connection's driver name.

See Also:
addDatabase, driver

exec

public final QSqlQuery exec()

Equivalent to exec(QString()).


exec

public final QSqlQuery exec(java.lang.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, lastError

hostName

public final java.lang.String hostName()

Returns the connection's host name. It may be empty.

See Also:
setHostName

isOpen

public final boolean isOpen()

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


isOpenError

public final boolean isOpenError()

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


isValid

public final boolean 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


lastError

public final QSqlError 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, QSqlQuery::lastError

open

public final boolean 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, setConnectOptions

open

public final boolean open(java.lang.String user,
                          java.lang.String password)

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

password

public final java.lang.String password()

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.

See Also:
setPassword

port

public final int port()

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

See Also:
setPort

primaryIndex

public final QSqlIndex primaryIndex(java.lang.String tablename)

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

See Also:
tables, record

record

public final QSqlRecord record(java.lang.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.


rollback

public final boolean rollback()

Rolls a transaction back on the database if the driver supports transactions and a transaction has been started. Returns true if the operation succeeded; otherwise returns false.

See Also:
QSqlDriver::hasFeature, commit

setConnectOptions

public final void setConnectOptions()

Equivalent to setConnectOptions(QString()).


setConnectOptions

public final void setConnectOptions(java.lang.String options)

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
  • CLIENT_COMPRESS
  • CLIENT_FOUND_ROWS
  • CLIENT_IGNORE_SPACE
  • CLIENT_SSL
  • CLIENT_ODBC
  • CLIENT_NO_SCHEMA
  • CLIENT_INTERACTIVE
  • UNIX_SOCKET
  • connect_timeout
  • options
  • tty
  • requiressl
  • service
DB2OCITDS
  • SQL_ATTR_ACCESS_MODE
  • SQL_ATTR_LOGIN_TIMEOUT
  • OCI_ATTR_PREFETCH_ROWS
  • OCI_ATTR_PREFETCH_MEMORY
none
SQLiteInterbase
  • QSQLITE_BUSY_TIMEOUT
  • ISC_DPB_LC_CTYPE
  • ISC_DPB_SQL_ROLE_NAME

Examples:

    ...
    // MySQL connection
    db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1"); // use an SSL connection to the server
    if (!db.open()) {
        db.setConnectOptions(); // clears the connect option string
        ...
    }
    ...
    // PostgreSQL connection
    db.setConnectOptions("requiressl=1"); // enable PostgreSQL SSL connections
    if (!db.open()) {
        db.setConnectOptions(); // clear options
        ...
    }
    ...
    // ODBC connection
    db.setConnectOptions("SQL_ATTR_ACCESS_MODE=SQL_MODE_READ_ONLY;SQL_ATTR_TRACE=SQL_OPT_TRACE_ON"); // set ODBC options
    if (!db.open()) {
        db.setConnectOptions(); // don't try to set this option
        ...
    }

Refer to the client library documentation for more information about the different options.

See Also:
connectOptions

setDatabaseName

public final void setDatabaseName(java.lang.String name)

Sets the connection's name to name. 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 name is database-specific.

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.

See Also:
databaseName, setUserName, setPassword, setHostName, setPort, setConnectOptions, open

setHostName

public final void setHostName(java.lang.String host)

Sets the connection's host name to host. 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).

There is no default value.

See Also:
hostName, setUserName, setPassword, setDatabaseName, setPort, setConnectOptions, open

setPassword

public final void setPassword(java.lang.String password)

Sets the connection's password to password. 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).

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.

See Also:
password, setUserName, setDatabaseName, setHostName, setPort, setConnectOptions, open

setPort

public final void setPort(int p)

Sets the connection's port number to p. 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).

There is no default value.

setDatabaseName setConnectOptions open

See Also:
port, setUserName, setPassword, setHostName

setUserName

public final void setUserName(java.lang.String name)

Sets the connection's user name to name. 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).

There is no default value.

setPort setConnectOptions open

See Also:
userName, setDatabaseName, setPassword, setHostName

tables

public final java.util.List<java.lang.String> tables()
This is an overloaded method provided for convenience.


tables

public final java.util.List<java.lang.String> tables(QSql.TableType type)

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

See Also:
primaryIndex, record

transaction

public final boolean transaction()

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

See Also:
QSqlDriver::hasFeature, commit, rollback

userName

public final java.lang.String userName()

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

See Also:
setUserName

addDatabase

public static QSqlDatabase addDatabase(java.lang.String type)

Equivalent to addDatabase(type, QLatin1String( defaultConnection )).


addDatabase

public static QSqlDatabase addDatabase(java.lang.String type,
                                       java.lang.String connectionName)

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 connectionName is not specified, the newly added database connection becomes the default database connection for the application, and subsequent calls to database without a database name parameter will return a reference to it. If connectionName is given, use database(connectionName) to retrieve a pointer to the database connection.

Warning: If you add a database with the same name as an existing database, the new database will replace the old one. This will happen automatically if you call this function more than once without specifying connectionName.

To make use of the connection, you will need to set it up, for example by calling some or all of setDatabaseName, setUserName, setPassword, setHostName, setPort, and setConnectOptions, and then you'll need to open the connection.

See Also:
database, removeDatabase, Threads and the SQL Module

addDatabase

public static QSqlDatabase addDatabase(QSqlDriver driver)

Equivalent to addDatabase(driver, QLatin1String( defaultConnection )).


addDatabase

public static QSqlDatabase addDatabase(QSqlDriver driver,
                                       java.lang.String connectionName)

This function is useful if you need to set up the database connection and instantiate the driver yourself. If you do this, it is recommended that you include the driver code in your own application. For example, setting up a custom PostgreSQL connection and instantiating the QPSQL driver can be done like this:

    #include "qtdir/src/sql/drivers/psql/qsql_psql.cpp"

(We assume that qtdir 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.

    PGconn *con = PQconnectdb("host=server user=bart password=simpson dbname=springfield");
    QPSQLDriver *drv =  new QPSQLDriver(con);
    QSqlDatabase db = QSqlDatabase::addDatabase(drv); // becomes the new default connection
    QSqlQuery query;
    query.exec("SELECT NAME, ID FROM STAFF");
    ...

The above code sets up a PostgreSQL connection and instantiates a QPSQLDriver object. Next, addDatabase 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.

Remember that you must link your application against the database client library as well. The simplest way to do this is to add lines like the ones below to your .pro file:

    unix:LIBS += -lpq
    win32:LIBS += libpqdll.lib

You will need to have the client library in your linker's search path.

The method described above will work for all the drivers, the only difference is the arguments the driver constructors take. Below is an overview of the drivers and their constructor arguments.

DriverClass nameConstructor argumentsFile to include
QPSQLQPSQLDriverPGconn *connectionqsql_psql.cpp
QMYSQLQMYSQLDriverMYSQL *connectionqsql_mysql.cpp
QOCIQOCIDriverOCIEnv *environment, OCISvcCtx *serviceContextqsql_oci.cpp
QODBCQODBCDriverSQLHANDLE environment, SQLHANDLE connectionqsql_odbc.cpp
QDB2QDB2SQLHANDLE environment, SQLHANDLE connectionqsql_db2.cpp
QTDSQTDSDriverLOGINREC *loginRecord, DBPROCESS *dbProcess, const QString &hostNameqsql_tds.cpp
QSQLITEQSQLiteDriversqlite *connectionqsql_sqlite.cpp
QIBASEQIBaseDriverisc_db_handle connectionqsql_ibase.cpp

The host name (or service name) is needed when constructing the QTDSDriver for creating new connections for internal queries. This is to prevent the simultaneous usage of several QSqlQuery objects from blocking each other.

Warning: If you add a database with the same name as an existing database, the new database will replace the old one.

Warning: The SQL framework takes ownership of the driver pointer, and it should not be deleted. If you want to explicitly remove the connection, use removeDatabase.

See Also:
drivers

cloneDatabase

public static QSqlDatabase cloneDatabase(QSqlDatabase other,
                                         java.lang.String connectionName)

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 that the connection is not opened, to use it, it is necessary to call open first.


connectionNames

public static java.util.List<java.lang.String> connectionNames()

Returns a list containing the names of all connections.

See Also:
contains, database, Threads and the SQL Module

contains

public static boolean contains()

Equivalent to contains(QLatin1String( defaultConnection )).


contains

public static boolean contains(java.lang.String connectionName)

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

See Also:
connectionNames, database, Threads and the SQL Module

database

public static QSqlDatabase database(java.lang.String connectionName)

Equivalent to database(connectionName, true).


database

public static QSqlDatabase database()

Equivalent to database(QLatin1String( defaultConnection ), true).


database

public static QSqlDatabase database(java.lang.String connectionName,
                                    boolean open)

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.

See Also:
isOpen, Threads and the SQL Module

drivers

public static java.util.List<java.lang.String> drivers()

Returns a list of all the available database drivers.

See Also:
registerSqlDriver

isDriverAvailable

public static boolean isDriverAvailable(java.lang.String name)

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

See Also:
drivers

registerSqlDriver

public static void registerSqlDriver(java.lang.String name,
                                     QSqlDriverCreatorBase creator)

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

removeDatabase

public static void removeDatabase(java.lang.String connectionName)

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

See Also:
database, Threads and the SQL Module

fromNativePointer

public static QSqlDatabase fromNativePointer(QNativePointer nativePointer)
This function returns the QSqlDatabase instance pointed to by nativePointer

Parameters:
nativePointer - the QNativePointer of which object should be returned.

nativePointerArray

public static QNativePointer nativePointerArray(QSqlDatabase[] array)
This function returns a QNativePointer that is pointing to the specified QSqlDatabase array.

Parameters:
array - the array that the returned pointer will point to.
Returns:
a QNativePointer that is pointing to the specified array.

defaultConnection

public static java.lang.String defaultConnection()
Returns this QSqlDatabase's default connection.


Qt Jambi Home