Qt database connection

Qt database connection

Qt is connected to a database accessed through the database .
 QSqlDatabase class provides an interface to access the database.

About QSqlDatabase

 Examples QSqlDatabase represents a connection to a database. Each connection supported by Qt database driver provides access to the database (database driver is derived from QSqlDriver). You can also write your own database-driven.
 By calling the static addDatabase () driver (database drivers) to specify a function to be used or the type of driver (depending on the type of database) and connection name (connectionName).

[static] QSqlDatabase QSqlDatabase::addDatabase(const QString &type, const QString &connectionName = QLatin1String(defaultConnection))

Each connection is through a connection name and database connection. And a database may have a plurality of database connections.
QSqlDatabase support the default connection, that unnamed connection (only need to specify the type of drive) . Create a default connection, just in addDatabase (connection name can not pass the time). After that, you call any static member function with no connection name is thought to use the default connection.


      QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
      db.setHostName("acidalia");
      db.setDatabaseName("customdb");
      db.setUserName("mojito");
      db.setPassword("J0a1m8");
      bool ok = db.open();

 The above code shows how to create and open the PostgreSQL default connection. After QSqlDatabase objects created using setDatabaseName (), setUserName (), setPassword (), setHostName (), setPort () and setConnectOptions () Set the connection parameters. Then open () call to open a physical connection to the database, before opening, the connection is unavailable .
 Defined above connection is the default connection, since no connection name is addDatabase (), after the default connection can be obtained by calling the database (), without the need to connect the name of the parameter.

	QSqlDatabase db = QSqlDatabase::database();

 QSqlDatabase value is a class (value class). Database connections made by a QSqlDatabase showing another example of the operation will affect QSqlDatabase instances of the same connection. Use cloneDatabase () to create a connection based on an existing database connection.
Warning: It is strongly recommended not to copy QSqlDatabase as a class member , as this will prevent proper clean up instances when closed. If you need to access an existing QSqlDatabase, you should use database () access. If you choose to use QSqlDatabase as a member variable, you need to remove it before deleting QCoreApplication instance, could cause undefined behavior.

 If you create multiple database connections, when you call addDatabase () for each connection to specify a unique connection name. Connection with the use of database name () to obtain the connection. Connection name for use removeDatabase () to delete a connection, if you try to delete the connection referenced by other QSqlDatabase object, QSqlDatabase output a warning. Whether to use contains () to view the connection name specified in the database connection list.

Note the use of multi-threaded database

 When using Qt, often with a variety of data processing GUI thread thread separately, then the multithreaded operations involve database.

Threads and the SQL Module
A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.
In addition, the third party libraries used by the QSqlDrivers can impose further restrictions on using the SQL Module in a multithreaded program. Consult the manual of your database client for more information

 Official documents have a clear description of each connection can only be used in the thread that created it, it does not support cross-threaded .
 If you try to cross-thread use the same database connection, you may receive the following warning,

QSqlDatabasePrivate::database: requested database does not belong to the calling thread.

 A connecting different cross-thread operation, does not mean that can not operate a multi-threaded database. Very simple to use, create a different connection or destroy the original connection again.

Published 60 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/BadAyase/article/details/103726147