Detailed Description
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. If 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 cloneConnection() 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.
Bookmarks