|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.sql.QSqlDatabase
public class QSqlDatabase
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 MySQL database:
QSqlDatabase db = QSqlDatabase.addDatabase("QPSQL"); db.setHostName("apollo"); db.setDatabaseName("customdb"); db.setUserName("mojito"); db.setPassword("J0a1m8"); boolean 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()
.
QSqlDriver
, QSqlQuery
, QtSql Module, and Threads and the SQL Module.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9 |
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal |
Field Summary |
---|
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
currentSender |
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)
This overload is useful when you want to create a database connection with a driver you instantiated yourself. |
static QSqlDatabase |
addDatabase(QSqlDriver driver,
java.lang.String connectionName)
This overload is useful when you want to create a database connection with a driver you instantiated yourself. |
static QSqlDatabase |
addDatabase(java.lang.String type)
Adds a database to the list of database connections using the driver type and the connection name connectionName. |
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. |
QSqlDatabase |
clone()
This method is reimplemented for internal reasons |
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. |
java.lang.String |
connectionName()
Returns the connection name, which may be empty. |
static java.util.List |
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()
Returns true if the list of database connections contains connectionName; otherwise returns false. |
static boolean |
contains(java.lang.String connectionName)
Returns true if the list of database connections contains connectionName; otherwise returns false. |
static QSqlDatabase |
database()
Returns the database connection called connectionName. |
static QSqlDatabase |
database(java.lang.String connectionName)
Returns the database connection called connectionName. |
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, which 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 |
drivers()
Returns a list of all the available database drivers. |
QSqlQuery |
exec()
Executes a SQL statement on the database and returns a QSqlQuery object. |
QSqlQuery |
exec(java.lang.String query)
Executes a SQL statement on the database and returns a QSqlQuery object. |
java.lang.String |
hostName()
Returns the connection's host name; it may be empty. |
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. |
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 back a transaction on the database, if the driver supports transactions and a transaction() has been started. |
void |
setConnectOptions()
Sets database-specific options. |
void |
setConnectOptions(java.lang.String options)
Sets database-specific options. |
void |
setDatabaseName(java.lang.String name)
Sets the connection's database 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 port. |
void |
setUserName(java.lang.String name)
Sets the connection's user name to name. |
java.util.List |
tables()
This is an overloaded method provided for convenience. |
java.util.List |
tables(QSql.TableType type)
Returns a list of the database's tables, system tables and views, as specified by the parameter type. |
java.lang.String |
toString()
|
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, equals, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
__qt_signalInitialization |
Methods inherited from class java.lang.Object |
---|
getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
public QSqlDatabase()
addDatabase()
, removeDatabase()
, and database()
to get valid QSqlDatabase objects.
protected QSqlDatabase(QSqlDriver driver)
public QSqlDatabase(QSqlDatabase other)
protected QSqlDatabase(java.lang.String type)
The currently available driver types are:
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 |
registerSqlDriver()
, and drivers()
.
Method Detail |
---|
public final void close()
QSqlQuery
objects that are used with the database. This will also affect copies of this QSqlDatabase object.
removeDatabase()
.
public final boolean commit()
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.
QSqlQuery::isActive()
, QSqlDriver::hasFeature()
, and rollback()
.
public final java.lang.String connectOptions()
setConnectOptions()
.
public final java.lang.String connectionName()
database name
. addDatabase()
.
public final java.lang.String databaseName()
setDatabaseName()
.
public final QSqlDriver driver()
addDatabase()
, and drivers()
.
public final java.lang.String driverName()
addDatabase()
, and driver()
.
public final QSqlQuery exec()
QSqlQuery
object. Use lastError()
to retrieve error information. If query is empty, an empty, invalid query is returned and lastError()
is not affected. QSqlQuery
, and lastError()
.
public final QSqlQuery exec(java.lang.String query)
QSqlQuery
object. Use lastError()
to retrieve error information. If query is empty, an empty, invalid query is returned and lastError()
is not affected. QSqlQuery
, and lastError()
.
public final java.lang.String hostName()
setHostName()
.
public final boolean isOpen()
public final boolean isOpenError()
lastError()
function.
public final boolean isValid()
Example:
QSqlDatabase db = new QSqlDatabase(); System.out.println(db.isValid()); // Returns false db = QSqlDatabase.database("sales"); System.out.println(db.isValid()); // Returns true if "sales" connection exists QSqlDatabase.removeDatabase("sales"); System.out.println(db.isValid()); // Returns false
public final QSqlError lastError()
Failures that occur in conjunction with an individual query are reported by QSqlQuery::lastError()
.
QSqlError
, and QSqlQuery::lastError()
.
public final boolean open()
lastError()
. lastError()
, setDatabaseName()
, setUserName()
, setPassword()
, setHostName()
, setPort()
, and setConnectOptions()
.
public final boolean open(java.lang.String user, java.lang.String password)
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.
lastError()
.
public final java.lang.String password()
setPassword()
, and if the password was given in the open()
call, or if no password was used, an empty string is returned. setPassword()
.
public final int port()
setPort()
.
public final QSqlIndex primaryIndex(java.lang.String tablename)
QSqlIndex
is returned. tables()
, and record()
.
public final QSqlRecord record(java.lang.String tablename)
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.
public final boolean rollback()
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.
QSqlQuery::isActive()
, QSqlDriver::hasFeature()
, and commit()
.
public final void setConnectOptions()
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:
|
|
|
|
| none |
|
|
// ... // 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.
connectOptions()
.
public final void setConnectOptions(java.lang.String options)
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:
|
|
|
|
| none |
|
|
// ... // 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.
connectOptions()
.
public final void setDatabaseName(java.lang.String name)
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.
databaseName()
, setUserName()
, setPassword()
, setHostName()
, setPort()
, setConnectOptions()
, and open()
.
public final void setHostName(java.lang.String host)
opened
. Alternatively, you can close()
the connection, set the host name, and call open()
again. There is no default value.
hostName()
, setUserName()
, setPassword()
, setDatabaseName()
, setPort()
, setConnectOptions()
, and open()
.
public final void setPassword(java.lang.String password)
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.
password()
, setUserName()
, setDatabaseName()
, setHostName()
, setPort()
, setConnectOptions()
, and open()
.
public final void setPort(int p)
opened
. Alternatively, you can close()
the connection, set the port number, and call open()
again.. There is no default value.
port()
, setUserName()
, setPassword()
, setHostName()
, setDatabaseName()
, setConnectOptions()
, and open()
.
public final void setUserName(java.lang.String name)
opened
. Alternatively, you can close()
the connection, set the user name, and call open()
again. There is no default value.
userName()
, setDatabaseName()
, setPassword()
, setHostName()
, setPort()
, setConnectOptions()
, and open()
.
public final java.util.List tables()
public final java.util.List tables(QSql.TableType type)
primaryIndex()
, and record()
.
public final boolean transaction()
QSqlDriver::hasFeature()
, commit()
, and rollback()
.
public final java.lang.String userName()
setUserName()
.
public static QSqlDatabase addDatabase(QSqlDriver driver)
driver
you instantiated yourself. It might be your own database driver, or you might just need to instantiate one of the Qt drivers yourself. If you do this, it is recommended that you include the driver code in your application. For example, you can create a PostgreSQL connection with your own QPSQL driver like this: #include "qtdir/src/sql/drivers/psql/qsql_psql.cpp"The above code sets up a PostgreSQL connection and instantiates a QPSQLDriver object. Next,
PGconn on = PQconnectdb("host=server user=bart password=simpson dbname=springfield"); QPSQLDriver rv = new QPSQLDriver(con); QSqlDatabase db = QSqlDatabase.addDatabase(drv); // becomes the new default connection QSqlQuery query; query.exec("SELECT NAME, ID FROM STAFF"); // ...
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. Note: 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.
Remember that you must link your application against the database client library. Make sure the client library is in your linker's search path, and add lines like these to your .pro file:
unix:LIBS += -lpq win32:LIBS += libpqdll.libThe method described works for all the supplied drivers. The only difference will be in the driver constructor arguments. Here is a table of the drivers included with Qt, their source code files, and their constructor arguments:
QPSQL | QPSQLDriver | PGconn *connection | qsql_psql.cpp |
QMYSQL | QMYSQLDriver | MYSQL *connection | qsql_mysql.cpp |
QOCI | QOCIDriver | OCIEnv *environment, OCISvcCtx *serviceContext | qsql_oci.cpp |
QODBC | QODBCDriver | SQLHANDLE environment, SQLHANDLE connection | qsql_odbc.cpp |
QDB2 | QDB2 | SQLHANDLE environment, SQLHANDLE connection | qsql_db2.cpp |
QTDS | QTDSDriver | LOGINREC *loginRecord, DBPROCESS *dbProcess, const QString &hostName | qsql_tds.cpp |
QSQLITE | QSQLiteDriver | sqlite *connection | qsql_sqlite.cpp |
QIBASE | QIBaseDriver | isc_db_handle connection | qsql_ibase.cpp |
QSqlQuery
objects are used simultaneously. Warning: Adding a database connection with the same connection name as an existing connection, causes the existing connection to be replaced by the new one.
Warning: The SQL framework takes ownership of the driver. It must not be deleted. To remove the connection, use removeDatabase()
.
drivers()
.
public static QSqlDatabase addDatabase(QSqlDriver driver, java.lang.String connectionName)
driver
you instantiated yourself. It might be your own database driver, or you might just need to instantiate one of the Qt drivers yourself. If you do this, it is recommended that you include the driver code in your application. For example, you can create a PostgreSQL connection with your own QPSQL driver like this: #include "qtdir/src/sql/drivers/psql/qsql_psql.cpp"The above code sets up a PostgreSQL connection and instantiates a QPSQLDriver object. Next,
PGconn on = PQconnectdb("host=server user=bart password=simpson dbname=springfield"); QPSQLDriver rv = new QPSQLDriver(con); QSqlDatabase db = QSqlDatabase.addDatabase(drv); // becomes the new default connection QSqlQuery query; query.exec("SELECT NAME, ID FROM STAFF"); // ...
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. Note: 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.
Remember that you must link your application against the database client library. Make sure the client library is in your linker's search path, and add lines like these to your .pro file:
unix:LIBS += -lpq win32:LIBS += libpqdll.libThe method described works for all the supplied drivers. The only difference will be in the driver constructor arguments. Here is a table of the drivers included with Qt, their source code files, and their constructor arguments:
QPSQL | QPSQLDriver | PGconn *connection | qsql_psql.cpp |
QMYSQL | QMYSQLDriver | MYSQL *connection | qsql_mysql.cpp |
QOCI | QOCIDriver | OCIEnv *environment, OCISvcCtx *serviceContext | qsql_oci.cpp |
QODBC | QODBCDriver | SQLHANDLE environment, SQLHANDLE connection | qsql_odbc.cpp |
QDB2 | QDB2 | SQLHANDLE environment, SQLHANDLE connection | qsql_db2.cpp |
QTDS | QTDSDriver | LOGINREC *loginRecord, DBPROCESS *dbProcess, const QString &hostName | qsql_tds.cpp |
QSQLITE | QSQLiteDriver | sqlite *connection | qsql_sqlite.cpp |
QIBASE | QIBaseDriver | isc_db_handle connection | qsql_ibase.cpp |
QSqlQuery
objects are used simultaneously. Warning: Adding a database connection with the same connection name as an existing connection, causes the existing connection to be replaced by the new one.
Warning: The SQL framework takes ownership of the driver. It must not be deleted. To remove the connection, use removeDatabase()
.
drivers()
.
public static QSqlDatabase addDatabase(java.lang.String type)
The database connection is referred to by connectionName. The newly added database connection is returned.
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()
.
database()
, removeDatabase()
, and Threads and the SQL Module.
public static QSqlDatabase addDatabase(java.lang.String type, java.lang.String connectionName)
The database connection is referred to by connectionName. The newly added database connection is returned.
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()
.
database()
, removeDatabase()
, and Threads and the SQL Module.
public static QSqlDatabase cloneDatabase(QSqlDatabase other, java.lang.String connectionName)
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()
.
public static java.util.List connectionNames()
contains()
, database()
, and Threads and the SQL Module.
public static boolean contains()
connectionNames()
, database()
, and Threads and the SQL Module.
public static boolean contains(java.lang.String connectionName)
connectionNames()
, database()
, and Threads and the SQL Module.
public static QSqlDatabase database(java.lang.String connectionName)
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. isOpen()
, and Threads and the SQL Module.
public static QSqlDatabase database()
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. isOpen()
, and Threads and the SQL Module.
public static QSqlDatabase database(java.lang.String connectionName, boolean open)
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. isOpen()
, and Threads and the SQL Module.
public static java.util.List drivers()
registerSqlDriver()
.
public static boolean isDriverAvailable(java.lang.String name)
drivers()
.
public static void registerSqlDriver(java.lang.String name, QSqlDriverCreatorBase creator)
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.
drivers()
.
public static void removeDatabase(java.lang.String connectionName)
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 = new QSqlQuery("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 setThe correct way to do it:
{ QSqlDatabase db = QSqlDatabase.database("sales"); QSqlQuery query = new QSqlQuery("SELECT NAME, DOB FROM EMPLOYEES", db); } // Both "db" and "query" are destroyed because they are out of scope QSqlDatabase.removeDatabase("sales"); // correctTo 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. database()
, connectionName()
, and Threads and the SQL Module.
public static java.lang.String defaultConnection()
public java.lang.String toString()
toString
in class java.lang.Object
public QSqlDatabase clone()
clone
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |