连接到数据库

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.

要使用QSqlQuery或QSqlQueryModel访问数据库,请创建并打开一个或多个数据库连接。数据库连接通常由连接名称标识,而不是由数据库名称标识。您可以有多个到同一数据库的连接。QSqlDatabase还支持默认连接的概念,即未命名的连接。当调用带有连接名称参数的QSqlQuery或QSqlQueryModel成员函数时,如果不传递连接名称,则将使用默认连接。当应用程序只需要一个数据库连接时,创建默认连接很方便。

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的实例。在打开连接之前,该连接不可用。以下片段显示了如何创建默认连接,然后打开它:

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.

第一行创建连接对象,最后一行打开连接对象以供使用。在这两者之间,我们初始化一些连接信息,包括数据库名、主机名、用户名和密码。在这种情况下,我们连接到主机bigblue上的MySQL数据库flightdb。addDatabase()的“QMYSQL”参数指定用于连接的数据库驱动程序的类型。Qt中包含的一组数据库驱动程序显示在支持的数据库驱动程序表中。

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

片段中的连接将是默认连接,因为我们没有将第二个参数传递给addDatabase(),这是连接名称。例如,我们在这里建立了两个MySQL数据库连接,分别命名为“first”和“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.

初始化这些连接后,为每个连接open()以建立活动连接。如果open()失败,则返回false。在这种情况下,调用QSqlDatabase::lastError()来获取错误信息。

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::database(),以获取指向该数据库连接的指针。如果我们不传递连接名称,它将返回默认连接。例如:

    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().

要删除数据库连接,请首先使用QSqlDatabase::close()关闭数据库,然后使用静态方法QSqlDatabase::removeDatabase()将其删除。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值