QT——QSqlQuery

本文详细介绍了QSqlQuery类的功能及使用方法,包括执行SQL语句、数据检索与导航等核心操作。并提供了实用示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The QSqlQuery class provides a means of executing and manipulating SQL statements.
QSqlQuery 提供了一种可以执行和操作SQL语句的方法。
=========================================================
QSqlQuery encapsulates the functionality involved in creating, navigating and retrieving data from SQL queries which are executed on a QSqlDatabase. It can be used to execute DML (data manipulation language) statements, such as SELECT, INSERT, UPDATE and DELETE, as well as DDL (data definition language) statements, such as CREATE TABLE. It can also be used to execute database-specific commands which are not standard SQL (e.g. SET DATESTYLE=ISO for PostgreSQL).
QSqlQuery 封装了包括创建、导航和索取数据的功能,数据来自于在QSqlDatabase数据库上执行的SQL的请求
它可以执行DML(数据操作语言)语句,比如 SELECT, INSERT, UPDATE 和 DELETE, 
还可以执行DDL(数据定义语言)语句,比如:CREATE TABLE。 
不仅如此,它还可以用于执行特定数据库而不是标准SQL语句的指令(比如 SET DATESTYLE=ISO 用于 PostgreSQL).

=========================================================
Successfully executed SQL statements set the query's state to active so that isActive() returns true. 
Otherwise the query's state is set to inactive. In either case, when executing a new SQL statement,the query is positioned on an invalid record.An active query must be navigated to a valid record (so that isValid() returns true) before values can be retrieved.
成功执行一SQL语句,设置query为活动状态( isActive() 返回TRUE),否则查询置为非活动状态。
无论那种情况,执行一条新的 SQL 语句之后,查询都被定位在一个无效的记录上;

所以在数据被获取之前,查询都必须被导航到一个有效的记录上 (isValid()可以确定查询是否处在一个有效的记录上)。

=========================================================

Navigating records is performed with the following functions:
用下列函数在记录之间进行导航:
next()
prev()
first()
last()
seek(int)

These functions allow the programmer to move forward, 
backward or arbitrarily through the records returned by the query. 
If you only need to move forward through the results (e.g., by using next()), 
you can use setForwardOnly(), which will save a significant amount of memory 
overhead and improve performance on some databases. 
Once an active query is positioned on a valid record, data can be retrieved using value(). 
All data is transferred from the SQL backend using QVariants.
这些函数允许程序员向前、向后或任意地在查询返回的记录之间移动。
一旦一个处于活动状态的查询 被定位在一个有效的记录上时,
就可以通过value()获取数据。
所有的数据都被用QVariants从SQL后端转换过来。
-------------------------------------------------------
比如:
    QSqlQuery query( "select name from customer" );
    while ( query.next() ) {
        QString name = query.value(0).toString();
        doSomething( name );
    }

 To access the data returned by a query, use value(int). 
 Each field in the data returned by a SELECT statement is accessed 
 by passing the field's position in the statement, starting from 0. 
 This makes using SELECT * queries inadvisable
 because the order of the fields returned is indeterminate.   
要获得从一个查询中返回的数据,用value()方法。
由SELECT语句返回的数据中的每个字段可以通过指定一个索引来访问,索引从0开始。
这里使得使用SELECT *查询不可取的,因为返回的域的顺序是不确定的。

For the sake of efficiency, there are no functions to access a field by name
(unless you use prepared queries with names, as explained below). 
To convert a field name into an index, use record().indexOf(), for example:
为了提高效率,没有通过 name 来获取一个字段的函数
(除非你使用名字制备的查询,如下的解释那样)。
通过使用record().indexOf( )将名字字段转换成一个索引,例如:
QSqlQuery query("SELECT * FROM artist");
int fieldNo = query.record().indexOf("country");
while (query.next()) {
QString country = query.value(fieldNo).toString();
doSomething(country);
}

=========================================================

bool QSqlQuery::next () [virtual]
Retrieves the next record in the result, if available, and positions the query on the retrieved record. 
Note that the result must be in the active state and isSelect() must return true before calling this function or it will do nothing and return false.
在结果集中检索下一个记录,如果有效,定位查询在被检索的记录上。
注意,在调用这个函数之前,结果集必须处于有效的状态,而且 isSelect() 必须返回 TRUE,
否则它将什么都不做返回 FALSE。

The following rules apply:
应用以下一些规则:

If the result is currently located before the first record, e.g. immediately after a query is executed,an attempt is made to retrieve the first record.
If the result is currently located after the last record, there is no change and false is returned.
If the result is located somewhere in the middle, an attempt is made to retrieve the next record.
1.如果如果集被定位在第一个记录之前,刚刚执行完一个查询就处于这种状态,那么可以用此函数来检索第一个记录。
2.如果如果集被定位在最后一个记录之后,那么什么都不会改变,返回FALSE。
3.如果结果集被定位在中间,此函数可以检索下一个记录。
4.如果记录不能被检索,结果集将被定位在最后一个记录之后,并返回 FALSE 。如果记录成功检索,那么返回 TRUE 。

=========================================================

QVariant QSqlQuery::value(int index) const
Returns the value of field index in the current record.
返回当前记录中index索引字段的值。
The fields are numbered from left to right using the text of the SELECT statement, e.g. in
用SELECT语句将 字段从左到右依次排列,例如:
SELECT forename, surname FROM people;
field 0 is forename and field 1 is surname. 
Using SELECT * is not recommended because the order of the fields in the query is undefined.
0字段为forename,1字段为surname。
使用SELECT * 是不被推荐的,因为查询中各字段排序是不明确的。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值