SQL in Qt (一)

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 adefault 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 supported database drivers.

The connection in the snippet will be the default connection, because we don't pass the second argument toaddDatabase(), which is the connection name. For example, here we establish two MySQL database connections named "first" and "second":

     QSqlDatabase firstDB = QSqlDatabase::addDatabase("QMYSQL", "first");

     QSqlDatabase secondDB = QSqlDatabase::addDatabase("QMYSQL", "second");

After these connections have been initialized, open() for each one to establish the live connections. If the open() fails, it returns false. In that case, call QSqlDatabase::lastError() to get error information.

Once a connection is established, we can call the static function QSqlDatabase::database() from anywhere with a connection name to get a pointer to that database connection. If we don't pass a connection name, it will return the default connection. For example:

     QSqlDatabase defaultDB = QSqlDatabase::database();

     QSqlDatabase firstDB = QSqlDatabase::database("first");

     QSqlDatabase secondDB = QSqlDatabase::database("second");

To remove a database connection, first close the database using QSqlDatabase::close(), then remove it using the static method QSqlDatabase::removeDatabase().

连接数据库

与QSqlQuery或QSqlQueryModel访问数据库,创建和打开一个或多个数据库连接。数据库连接通常被连接的名字,而不是数据库名称。可以有多个相同的数据库连接。QSqlDatabase还支持一个默认连接的概念,这是一个不愿透露姓名的连接。当调用QSqlQuery或QSqlQueryModel成员函数,把一个连接名称的参数,如果你不通过连接名称,将使用默认的连接。创建一个默认的连接方便当您的应用程序只需要一个数据库连接。

注意创建连接的区别和打开它。创建一个连接涉及创建QSqlDatabase类的一个实例。不是可用的,直到它被打开的连接。以下代码片段显示了如何创建adefault连接,然后打开它:

     QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

     db.setHostName("bigblue");

     db.setDatabaseName("flightdb");

     db.setUserName("acarlson");

     db.setPassword("1uTbSbAs");

     bool ok = db.open();

创建连接对象,第一行和最后一行打开使用。在之间,我们初始化一些连接信息,包括数据库名称、主机名、用户名和密码。在这个例子中,我们连接到MySQL数据库主机bigblue flightdb。“QMYSQL”参数addDatabase()指定类型的数据库驱动程序用于连接。数据库驱动程序集包含在Qt支持的数据库驱动程序表所示。 片段的连接将被默认的连接,因为我们不通过第二个参数toaddDatabase(),它是连接的名字。例如,在这里我们建立两个MySQL数据库连接命名“第一”和“第二”:

     QSqlDatabase firstDB = QSqlDatabase::addDatabase("QMYSQL", "first");

     QSqlDatabase secondDB = QSqlDatabase::addDatabase("QMYSQL", "second");

这些连接被初始化后,open()为每个人建立现场连接。如果开放()失败,它将返回错误的。在这种情况下,调用QSqlDatabase:lastError()来获取错误信息。 一旦建立连接,我们可以调用静态函数QSqlDatabase::数据库()从任何连接的名字让指针,数据库连接。如果我们不通过连接的名字,它将返回默认的连接。例如:

     QSqlDatabase defaultDB = QSqlDatabase::database();

     QSqlDatabase firstDB = QSqlDatabase::database("first");

     QSqlDatabase secondDB = QSqlDatabase::database("second");

先删除一个数据库连接,关闭数据库使用QSqlDatabase::关闭(),然后使用静态方法QSqlDatabase删除它:removeDatabase()。

你可能感兴趣的:(sql)