http://www.voidcn.com/article/p-kuwabvfy-bms.html
https://blog.csdn.net/kilotwo/article/details/79248657
http://blog.sina.com.cn/s/blog_a6fb6cc90101h1gr.html
https://doc.qt.io/qt-5/sql-connecting.html
Connecting to Databases
To access a database with QSqlQuery or QSqlQueryModel, create and open one or more database connections. Database connections are normally identified by connection name, not by database name. You can have multiple connections to the same database. QSqlDatabase also supports the concept of a default connection, which is an unnamed connection. When calling QSqlQuery or QSqlQueryModel member functions that take a connection name argument, if you don't pass a connection name, the default connection will be used. Creating a default connection is convenient when your application only requires one database connection.
Note the difference between creating a connection and opening it. Creating a connection involves creating an instance of class QSqlDatabase. The connection is not usable until it is opened. The following snippet shows how to create a default connection and then open it:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("bigblue");
db.setDatabaseName("flightdb");
db.setUserName("acarlson");
db.setPassword("1uTbSbAs");
bool ok = db.open();
The first line creates the connection object, and the last line opens it for use. In between, we initialize some connection information, including the database name, the host name, the user name, and the password. In this case, we are connecting to the MySQL database flightdb
on the host bigblue
. The "QMYSQL"
argument to addDatabase() specifies the type of database driver to use for the connection. The set of database drivers included with Qt are shown in the table of