QSqlQuery Class Reference

[QtSql module]

该QSqlQuery类提供执行和操作的SQL语句的方法。More…

Types

  • enum BatchExecutionMode { ValuesAsRows, ValuesAsColumns }

Methods

  • __init__ (self, QSqlResult r)
  • __init__ (self, QString query = QString(), QSqlDatabase db = QSqlDatabase())
  • __init__ (self, QSqlDatabase db)
  • __init__ (self, QSqlQuery other)
  • addBindValue (self, QVariant val, QSql.ParamType type = QSql.In)
  • int at (self)
  • bindValue (self, QString placeholder, QVariant val, QSql.ParamType type = QSql.In)
  • bindValue (self, int pos, QVariant val, QSql.ParamType type = QSql.In)
  • QVariant boundValue (self, QString placeholder)
  • QVariant boundValue (self, int pos)
  • dict-of-QString-QVariant boundValues (self)
  • clear (self)
  • QSqlDriver driver (self)
  • bool exec_ (self, QString query)
  • bool exec_ (self)
  • bool execBatch (self, BatchExecutionMode mode = QSqlQuery.ValuesAsRows)
  • QString executedQuery (self)
  • finish (self)
  • bool first (self)
  • bool isActive (self)
  • bool isForwardOnly (self)
  • bool isNull (self, int field)
  • bool isSelect (self)
  • bool isValid (self)
  • bool last (self)
  • QSqlError lastError (self)
  • QVariant lastInsertId (self)
  • QString lastQuery (self)
  • bool next (self)
  • bool nextResult (self)
  • QSql.NumericalPrecisionPolicy numericalPrecisionPolicy (self)
  • int numRowsAffected (self)
  • bool prepare (self, QString query)
  • bool previous (self)
  • QSqlRecord record (self)
  • QSqlResult result (self)
  • bool seek (self, int index, bool relative = False)
  • setForwardOnly (self, bool forward)
  • setNumericalPrecisionPolicy (self, QSql.NumericalPrecisionPolicy precisionPolicy)
  • int size (self)
  • QVariant value (self, int i)

Detailed Description

该QSqlQuery类提供执行和操作的SQL语句的方法。

QSqlQuery封装这是在执行涉及从SQL查询创建,浏览和检索数据的功能QSqlDatabase。它可以被用来执行DML (数据操纵语言)语句,如SELECTINSERTUPDATEDELETE,以及DDL (数据定义语言)语句,如CREATE TABLE。它也可以被用来执行特定于数据库的命令而不是标准的SQL (例如SET DATESTYLE=ISOPostgreSQL的) 。

成功执行的SQL语句设置查询的状态为主动,使isActive( )返回True 。否则,查询的状态设置为无效。在任一情况下,执行一个新的SQL语句时,查询被定位在一个无效的记录。一个活跃的查询必须被导航到一个有效的记录(使isValid( )值可以被检索之前返回True ) 。

对于某些数据库,如果积极的查询,它是一个SELECT当你调用语句存在commit() or rollback(),在提交或回滚将失败。看isActive( )了解详情。

导航记录的是具有以下功能进行:

这些功能使程序员可以前进,后退或者擅自通过查询返回的记录。如果你只需要向前移动的结果(例如,通过使用next( ) ) ,则可以使用setForwardOnly( ) ,这将节省一个显着的内存开销,提高对某些数据库的性能。一旦活动查询被定位在一个有效的记录,数据可以使用检索value( ) 。所有数据从SQL后端使用转QVariants

例如:

  1. QSqlQuery query("SELECT country FROM artist");
  2. while (query.next()) {
  3. [QString]($docs-qstring.html) country = query.value(0).toString();
  4. doSomething(country);
  5. }

要访问查询返回的数据,使用值(整数)。在由返回的数据中的每个字段SELECT声明是通过传递该领域的地位在声明中,从0开始访问的。这使得使用SELECT *查询不可取的,因为返回的字段的顺序是不确定的。

为了提高效率,有没有函数通过名字来访问一个字段(除非你使用预编译查询的姓名,解释见下文) 。若要将字段名成一个索引,使用record( ) 。indexOf()例如:

  1. QSqlQuery query("SELECT * FROM artist");
  2. int fieldNo = query.record().indexOf("country");
  3. while (query.next()) {
  4. [QString]($docs-qstring.html) country = query.value(fieldNo).toString();
  5. doSomething(country);
  6. }

QSqlQuery支持准备好的查询执行和参数值来佔位符绑定。有些数据库不支持这些功能,所以对于那些, Qt的模拟所需的功能。例如, Oracle和ODBC驱动程序已准备妥当查询支持,和Qt使得它的使用,但对于那些没有这种支持的数据库, Qt的实现了功能本身,如通过执行一个查询时用实际值替换的佔位符。使用numRowsAffected( )来找出多少行受到非SELECT查询,并size()找到多少被检索由SELECT

Oracle数据库通过使用一个冒号名称的语法,如查明佔位符:name。 ODBC只是使用?字符。 Qt支持这两种语法,用,你不能将它们混合在同一个查询的限制。

您可以使用检索所有单个变量的字段的值(地图)boundValues( ) 。

Approaches to Binding Values

下面,我们提出使用每四个不同的结合方式,以及结合值到存储过程的一个例子相同的例子。

Named binding using named placeholders:

  1. QSqlQuery query;
  2. query.prepare("INSERT INTO person (id, forename, surname) "
  3. "VALUES (:id, :forename, :surname)");
  4. query.bindValue(":id", 1001);
  5. query.bindValue(":forename", "Bart");
  6. query.bindValue(":surname", "Simpson");
  7. query.exec();

Positional binding using named placeholders:

  1. QSqlQuery query;
  2. query.prepare("INSERT INTO person (id, forename, surname) "
  3. "VALUES (:id, :forename, :surname)");
  4. query.bindValue(0, 1001);
  5. query.bindValue(1, "Bart");
  6. query.bindValue(2, "Simpson");
  7. query.exec();

Binding values using positional placeholders (version 1):

  1. QSqlQuery query;
  2. query.prepare("INSERT INTO person (id, forename, surname) "
  3. "VALUES (?, ?, ?)");
  4. query.bindValue(0, 1001);
  5. query.bindValue(1, "Bart");
  6. query.bindValue(2, "Simpson");
  7. query.exec();

Binding values using positional placeholders (version 2):

  1. QSqlQuery query;
  2. query.prepare("INSERT INTO person (id, forename, surname) "
  3. "VALUES (?, ?, ?)");
  4. query.addBindValue(1001);
  5. query.addBindValue("Bart");
  6. query.addBindValue("Simpson");
  7. query.exec();

Binding values to a stored procedure:

此代码调用一个存储过程调用AsciiToInt()在参数传递给它一个字符通过,并考虑其结果的输出参数。

  1. QSqlQuery query;
  2. query.prepare("CALL AsciiToInt(?, ?)");
  3. query.bindValue(0, "A");
  4. query.bindValue(1, 0, [QSql](qsql.html).Out);
  5. query.exec();
  6. int i = query.boundValue(1).toInt(); // i is 65

需要注意的是未绑定的参数将保留它们的值。

使用return语句返回值或返回多个结果集的存储过程,不完全支持。对于具体细节见SQL Database Drivers

Warning:你必须载入SQL驱动程序并创建一个QSqlQuery之前打开连接。此外,该连接必须保持开放,而存在的查询,否则, QSqlQuery的行为是未定义的。


Type Documentation

  1. QSqlQuery.BatchExecutionMode
Constant Value Description
QSqlQuery.ValuesAsRows 0 - 更新多行。黄柏中的每个条目QVariantList作为用于更新的下一行的值。
QSqlQuery.ValuesAsColumns 1 - 更新一行。黄柏中的每个条目QVariantList作为数组类型的单个值。

Method Documentation

  1. QSqlQuery.__init__ (self, QSqlResult r)

构造一个QSqlQuery对象,它使用了QSqlResult result与数据库进行通信。

  1. QSqlQuery.__init__ (self, QString query = QString(), QSqlDatabase db = QSqlDatabase())

构造一个QSqlQuery使用SQL对象query和数据库db。如果db如果未指定,或者是无效的,应用程序的默认数据库被使用。如果query是不是空字符串,它就会被执行。

See also QSqlDatabase

  1. QSqlQuery.__init__ (self, QSqlDatabase db)

构造一个QSqlQuery使用数据库对象db。如果db是无效的,应用程序的默认数据库将被使用。

See also QSqlDatabase

  1. QSqlQuery.__init__ (self, QSqlQuery other)

构造的副本other

  1. QSqlQuery.addBindValue (self, QVariant val, QSql.ParamType type = QSql.In)

增加值val使用位置值绑定时的值列表。该addBindValue ( )调用的顺序确定哪个佔位符的值将在准备好的查询绑定到。如果paramType is QSql.Out or QSql.InOut,佔位符将与后从数据库中的数据复盖exec_( )调用。

要绑定一个NULL值,使用空QVariant,例如,使用QVariant(QVariant.String)如果要绑定的字符串。

See also bindValue( )prepare( )exec_( )boundValue()和boundValues( ) 。

  1. int QSqlQuery.at (self)

返回查询的当前内部位置。第一个记录是在位置0 。如果位置是无效的,则函数返回QSql.BeforeFirstRow or QSql.AfterLastRow,这是特殊的负值。

See also previous( )next( )first( )last( )seek( )isActive()和isValid( ) 。

  1. QSqlQuery.bindValue (self, QString placeholder, QVariant val, QSql.ParamType type = QSql.In)

设置佔位符placeholder绑定到值val在准备好的语句。注意,佔位符标记(例如:)必须指定佔位符的名称时,必须包括在内。如果paramType is QSql.Out or QSql.InOut,佔位符将与后从数据库中的数据复盖exec_( )调用。在这种情况下,有足够的空间必须被预先分配给的结果存储到。

要绑定一个NULL值,使用空QVariant,例如,使用QVariant(QVariant.String)如果要绑定的字符串。

值不能绑定到多个位置的查询,例如:

  1. INSERT INTO testtable (id, name, samename) VALUES (:id, :name, :name)

绑定到名称将绑定到第一:名字,但没有第二个。

See also addBindValue( )prepare( )exec_( )boundValue()和boundValues( ) 。

  1. QSqlQuery.bindValue (self, int pos, QVariant val, QSql.ParamType type = QSql.In)

设置佔位符的位置pos绑定到值val在准备好的语句。字段编号从0开始。如果paramType is QSql.Out or QSql.InOut,佔位符将与后从数据库中的数据复盖exec_( )调用。

  1. QVariant QSqlQuery.boundValue (self, QString placeholder)

传回值placeholder

See also boundValues( )bindValue()和addBindValue( ) 。

  1. QVariant QSqlQuery.boundValue (self, int pos)

返回在位置的佔位符的值pos

  1. dict-of-QString-QVariant QSqlQuery.boundValues (self)

返回绑定值的地图。

与名为绑定,绑定的值可以检查以下方面:

  1. [QMapIterator]($docs-index.htm)<[QString]($docs-qstring.html), [QVariant]($docs-qvariant.html)> i(query.boundValues());
  2. while (i.hasNext()) {
  3. i.next();
  4. cout << i.key().toAscii().data() << ": "
  5. << i.value().toString().toAscii().data() << endl;
  6. }

与位置绑定,代码变为:

  1. [QList]($docs-index.htm)<[QVariant]($docs-qvariant.html)> list = query.boundValues().values();
  2. for (int i = 0; i < list.size(); ++i)
  3. cout << i << ": " << list.at(i).toString().toAscii().data() << endl;

See also boundValue( )bindValue()和addBindValue( ) 。

  1. QSqlQuery.clear (self)

清除结果集,并释放该查询持有的任何资源。设置查询状态为无效。你应该很少,如果需要调用这个函数。

  1. QSqlDriver QSqlQuery.driver (self)

[

返回与查询相关联的数据库驱动程序。

  1. bool QSqlQuery.exec_ (self, QString query)

]($docs-qsqldriver.html)

执行SQL中query。返回True ,并设置查询状态active如果查询成功,否则返回False 。该query字符串必须使用适当的语法为被查询(例如,标准的SQL )的SQL数据库。

查询被执行后,查询被定位在一个invalid记录,并且必须被导航到一个有效记录的数据值可以被检索(例如,在使用前next())。

请注意,此查询的最后一个错误,当执行exec( )被调用复位。

对SQLite的查询字符串可以一次只包含一个语句。如果多于一个语句被放弃,该函数返回False 。

例如:

  1. [QSqlQuery]($docs-qsqlquery.html) query;
  2. query.exec("INSERT INTO employee (id, name, salary) "
  3. "VALUES (1001, 'Thad Beaumont', 65000)");

See also isActive( )isValid( )next( )previous( )first( )last()和seek( ) 。

  1. bool QSqlQuery.exec_ (self)

执行先前准备的SQL查询。返回True如果成功执行该查询,否则返回False 。

请注意,此查询的最后一个错误是,当复位exec_()被调用。

See also prepare( )bindValue( )addBindValue( )boundValue()和boundValues( ) 。

  1. bool QSqlQuery.execBatch (self, BatchExecutionMode mode = QSqlQuery.ValuesAsRows)

执行先前准备的SQL查询中的批次。所有绑定参数必须是变种列表。如果数据库不支持批量处决,驱动程序将使用传统的模拟它exec_( )调用。

返回True如果查询成功执行,否则返回False 。

例如:

  1. [QSqlQuery]($docs-qsqlquery.html) q;
  2. q.prepare("insert into myTable values (?, ?)");
  3. [QVariantList]($docs-qvariant.html#QVariantList-typedef) ints;
  4. ints << 1 << 2 << 3 << 4;
  5. q.addBindValue(ints);
  6. [QVariantList]($docs-qvariant.html#QVariantList-typedef) names;
  7. names << "Harald" << "Boris" << "Trond" << [QVariant]($docs-qvariant.html)([QVariant]($docs-qvariant.html).String);
  8. q.addBindValue(names);
  9. if (!q.execBatch())
  10. qDebug() << q.lastError();

上面的例子中插入四个新行插入myTable

  1. 1 Harald
  2. 2 Boris
  3. 3 Trond
  4. 4 NULL

要绑定NULL值,空QVariant相关类型的已被添加到绑定QVariantList,例如,QVariant(QVariant.String)应该如果您使用的是字符串中使用。

Note:每一个绑定QVariantList必须包含的变体是相同的。

Note:的类型QVariants在列表中不能改变。例如,您不能在一个混合整数和字符串变量QVariantList

mode参数指示如何绑定QVariantList将被解释。如果mode is ValuesAsRows,内部的每一个变种QVariantList将被解释为一个新的行的值。ValuesAsColumns对于Oracle驱动程序的一个特例。在这种模式下,在一个每条目QVariantList将被解释为一个存储过程中的IN或OUT值的数组值。请注意,这只会工作,如果IN或OUT值是一个表型组成的基本型只有一列,例如TYPE myType IS TABLE OF VARCHAR(64) INDEX BY BINARY_INTEGER;

这个函数中引入了Qt 4.2中。

See also prepare( )bindValue()和addBindValue( ) 。

  1. QString QSqlQuery.executedQuery (self)

返回成功执行的最后一个查询。

在大多数情况下,这个函数返回相同的字符串lastQuery( ) 。如果有一个佔位符编写的查询上不支持它的DBMS执行,这个查询的制备效仿。在原始查询中的佔位符替换为它们的绑定值,形成一个新的查询。该函数返回修改后的查询。它是用于调试目的大多是有用的。

See also lastQuery( ) 。

  1. QSqlQuery.finish (self)

指示没有更多的数据将从这个查询被取出,直到重新执行它的数据库驱动程序。通常没有必要调用这个函数,但它可能会有所帮助,以免费资源,例如锁或游标,如果你打算重新使用查询在以后的时间。

设置查询为无效。绑定值保留它们的值。

此功能被引入Qt的4.3.2 。

See also prepare( )exec_()和isActive( ) 。

  1. bool QSqlQuery.first (self)

获取第一条记录的结果,如果有的话,和对检索到的记录位置的查询。请注意,其结果必然是在active状态isSelect()必须调用这个函数,否则将什么都不做,返回False才返回True。如果成功,则返回True 。如果不成功查询位置被设置为无效位置,返回False 。

See also next( )previous( )last( )seek( )at( )isActive()和isValid( ) 。

  1. bool QSqlQuery.isActive (self)

返回True如果查询active。一个活跃QSqlQuery是一个已经exec()’d成功但尚未完成。当你完成了一个活跃的查询,可以使使查询无效的调用finish()或clear( ) ,或者你可以删除QSqlQuery实例。

Note:特别感兴趣的是一种主动查询,它是一个SELECT声明。对于支持事务的一些数据库,积极查询,它是一个SELECT语句可以导致commit()rollback()失败,因此提交或回滚之前,你应该让你活跃SELECT语句查询无效使用上面列出的方法之一。

See also isSelect( ) 。

  1. bool QSqlQuery.isForwardOnly (self)

返回True如果你只能通过一个结果集向前滚动,否则返回False 。

See also setForwardOnly()和next( ) 。

  1. bool QSqlQuery.isNull (self, int field)

返回True如果查询active和定位在一个有效的记录和field为NULL ,否则返回False 。请注意,对于某些驱动程序, ISNULL( )将不会返回准确的信息后,才试图检索数据。

See also isActive( )isValid()和value( ) 。

  1. bool QSqlQuery.isSelect (self)

返回True如果当前的查询是SELECT声明,否则返回False 。

  1. bool QSqlQuery.isValid (self)

返回True如果查询是目前定位在一个有效的记录,否则返回False 。

  1. bool QSqlQuery.last (self)

检索记录集中的最后结果,如果有的话,和对检索到的记录位置的查询。请注意,其结果必然是在active状态isSelect()必须调用这个函数,否则将什么都不做,返回False才返回True。如果成功,则返回True 。如果不成功查询位置被设置为无效位置,返回False 。

See also next( )previous( )first( )seek( )at( )isActive()和isValid( ) 。

  1. QSqlError QSqlQuery.lastError (self)

[

返回有关最后一个错误(如果有的话) ,与此查询时发生的错误信息。

]($docs-qsqlerror.html)

See also QSqlErrorQSqlDatabase.lastError( ) 。

  1. QVariant QSqlQuery.lastInsertId (self)

返回最近插入的行的对象ID ,如果数据库支持它。无效的QVariant将被退回,如果查询没有插入任何值,或者如果数据库没有报告ID后面。如果不止一个行被感动的插入,该行为是未定义的。

对于MySQL数据库行的自动递增字段将被退回。

Note:对于这个功能在PSQL工作,该表的表必须包含的OID ,它可能没有被创建的默认。检查default_with_oids配置变量来确定。

See also QSqlDriver.hasFeature( ) 。

  1. QString QSqlQuery.lastQuery (self)

如果没有当前的查询文本返回当前查询所使用的文字,或空字符串。

See also executedQuery( ) 。

  1. bool QSqlQuery.next (self)

检索下一条记录中的结果,如果有的话,和对检索到的记录位置的查询。请注意,其结果必然是在active状态isSelect()必须调用这个函数,否则将什么都不做,返回False才返回True。

以下规则适用:

  • 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.

如果不能被检索的记录,结果是最后一条记录并返回False后定位。如果记录被成功取出,则返回True 。

See also previous( )first( )last( )seek( )at( )isActive()和isValid( ) 。

  1. bool QSqlQuery.nextResult (self)

丢弃当前的结果集,并导航到下一个(如果可用) 。

有些数据库是可以返回多个结果集的存储过程或SQL批处理(包含多个语句的查询字符串)的。如果在执行一个查询此功能可用于导航到下一个结果集(次)后,多个结果集可用。

如果一个新的结果集是可用此函数将返回True 。该查询将在一个重新定位invalid在新的结果集的记录,必须导航到一个有效的记录数据值可以被检索之前。如果一个新的结果集是不可用的函数返回False ,并且查询被设置为无效。在任何情况下,旧的结果集合将被丢弃。

当语句之一是一个非select语句影响的行计数可能,而不是一个结果集可用。

需要注意的是一些数据库,如微软的SQL Server ,要求非滚动游标与多个结果集时。有些数据库可以一次执行所有语句,而其他人可能会推迟执行,直到实际访问的结果集,以及一些数据库可能对哪些语句被允许在SQL批处理中使用的限制。

此功能被引入Qt的4.4 。

See also QSqlDriver.hasFeature( )setForwardOnly( )next( )isSelect( )numRowsAffected( )isActive()和lastError( ) 。

  1. QSql.NumericalPrecisionPolicy QSqlQuery.numericalPrecisionPolicy (self)

[

返回当前的精度政策。

]($docs-qsql.html#NumericalPrecisionPolicy-enum)

See also QSql.NumericalPrecisionPolicysetNumericalPrecisionPolicy( ) 。

  1. int QSqlQuery.numRowsAffected (self)

返回受影响的结果的SQL语句,或者-1 ,如果它不能确定的行数。请注意,对于SELECT语句,该值是不确定的;使用size( )来代替。如果该查询是不active,则返回-1。

See also size()和QSqlDriver.hasFeature( ) 。

  1. bool QSqlQuery.prepare (self, QString query)

准备SQL查询query用于执行。返回True如果查询成功制备,否则返回False 。

该查询可以包含佔位符绑定的值。这两个Oracle风格冒号名称(例如,:surname) ,和ODBC风格(?)佔位符的支持,但他们不能在同一查询中混用。请参阅Detailed Description为例子。

可移植性注:有些数据库选择延迟准备的查询,直到它被执行的第一次。在这种情况下,准备一个语法错误的查询成功,但每一个连续的exec_( )将失败。

对SQLite的查询字符串可以一次只包含一个语句。如果一个以上的语句给,函数返回False 。

例如:

  1. [QSqlQuery]($docs-qsqlquery.html) query;
  2. query.prepare("INSERT INTO person (id, forename, surname) "
  3. "VALUES (:id, :forename, :surname)");
  4. query.bindValue(":id", 1001);
  5. query.bindValue(":forename", "Bart");
  6. query.bindValue(":surname", "Simpson");
  7. query.exec();

See also exec_( )bindValue()和addBindValue( ) 。

  1. bool QSqlQuery.previous (self)

检索以前的记录结果中,如果有的话,以及对检索到的记录位置的查询。请注意,其结果必然是在active状态isSelect()必须调用这个函数,否则将什么都不做,返回False才返回True。

以下规则适用:

  • If the result is currently located before the first record, there is no change and false is returned.
  • If the result is currently located after the last record, an attempt is made to retrieve the last record.
  • If the result is somewhere in the middle, an attempt is made to retrieve the previous record.

如果不能被检索的记录,结果被定位在第一个记录并返回False之前。如果记录被成功取出,则返回True 。

See also next( )first( )last( )seek( )at( )isActive()和isValid( ) 。

  1. QSqlRecord QSqlQuery.record (self)

返回QSqlRecord包含域信息的当前查询。如果查询指向一个有效的行(isValid( )返回True ) ,该记录被填充了该行的值。当没有主动查询一个空的记录,则返回(isActive( )返回False ) 。

从查询中检索值,value( )应该被使用,因为它的基于索引的查找速度更快。

在下面的示例中,SELECT * FROM执行查询。由于列的顺序没有被定义,QSqlRecord.indexOf()是用来获取的列的索引。

  1. [QSqlQuery]($docs-qsqlquery.html) q("select * from employees");
  2. [QSqlRecord]($docs-qsqlrecord.html) rec = q.record();
  3. qDebug() << "Number of columns: " << rec.count();
  4. int nameCol = rec.indexOf("name"); // index of the field "name"
  5. while (q.next())
  6. qDebug() << q.value(nameCol).toString(); // output all names

See also value( ) 。

  1. QSqlResult QSqlQuery.result (self)

[

返回与查询相关的结果。

  1. bool QSqlQuery.seek (self, int index, bool relative = False)

]($docs-qsqlresult.html)

检索记录位置index如果有的话,和立场上所检索的记录的查询。第一个记录是在位置0 。请注意,查询必须是在active状态isSelect( )必须在调用这个函数之前返回True。

If relative为False(默认值) ,则适用以下规则:

  • If index is negative, the result is positioned before the first record and false is returned.
  • Otherwise, an attempt is made to move to the record at position index. If the record at position index could not be retrieved, the result is positioned after the last record and false is returned. If the record is successfully retrieved, true is returned.

If relative是真的,应遵守下列规则:

  • If the result is currently positioned before the first record or on the first record, and index is negative, there is no change, and false is returned.
  • If the result is currently located after the last record, and index is positive, there is no change, and false is returned.
  • If the result is currently located somewhere in the middle, and the relative offset index moves the result below zero, the result is positioned before the first record and false is returned.
  • Otherwise, an attempt is made to move to the record index records ahead of the current record (or index records behind the current record if index is negative). If the record at offset index could not be retrieved, the result is positioned after the last record if index >= 0, (or before the first record if index is negative), and false is returned. If the record is successfully retrieved, true is returned.

See also next( )previous( )first( )last( )at( )isActive()和isValid( ) 。

  1. QSqlQuery.setForwardOnly (self, bool forward)

设置只进模式forward。如果forward是真的,只next()和seek( )与正面的价值观,是允许的导航结果。

只正向模式即可(这取决于驱动程序)更多的内存效率,因为结果并不需要被缓存。它也将提高某些数据库的性能。对于这是真的,你必须调用setForwardOnly()查询准备或执行之前。需要注意的是,需要一个查询和数据库的构造函数可以执行查询。

只正向模式默认是关闭的。

前冲只为False是一个建议的数据库引擎,它拥有最终决定权在结果集是否是只向前或滚动。isForwardOnly( )将总是返回结果集的正确状态。

Note:调用setForwardOnly后执行的查询会导致意想不到的结果充其量和崩溃在最坏的情况。

See also isForwardOnly( )next( )seek()和QSqlResult.setForwardOnly( ) 。

  1. QSqlQuery.setNumericalPrecisionPolicy (self, QSql.NumericalPrecisionPolicy precisionPolicy)

指示数据库驱动程序返回的数值由指定的精度precisionPolicy

Oracle驱动程序,例如,可以检索数字值作为字符串来防止精度损失。如果精度高不要紧,使用此方法绕过字符串转换来提高执行速度。

注意:不支持用低精度取数值的驱动程序将忽略精度政策。您可以使用QSqlDriver.hasFeature( )来找出驱动程序是否支持此功能。

注意:设置精度策略不影响当前活动的查询。通话exec_QString)或prepare() ,以激活该策略。

See also QSql.NumericalPrecisionPolicynumericalPrecisionPolicy( ) 。

  1. int QSqlQuery.size (self)

返回结果的大小(返回的行数) ,或-1,如果大小无法确定,或者如果数据库不支持报告有关查询大小的信息。请注意,对于非SELECT报表(isSelect( )返回False ) ,大小( )将返回-1 。如果查询不活跃(isActive( )返回False ) ,则返回-1。

要确定一个非受影响的行数SELECT声明中,使用numRowsAffected( ) 。

See also isActive( )numRowsAffected()和QSqlDriver.hasFeature( ) 。

  1. QVariant QSqlQuery.value (self, int i)

返回字段的值index在当前的记录。

该字段使用的文本编号从左至右SELECT声明中,例如在

  1. SELECT forename, surname FROM people;

场0forename和现场1surname。运用SELECT *不建议,因为在查询中字段的顺序是不确定的。

无效的QVariant返回如果场index不存在,如果查询是无效的,或者如果该查询被定位在一个无效的记录。

See also previous( )next( )first( )last( )seek( )isActive()和isValid( ) 。