PySide数据库类学习QSqlQuery(一)

 

QSqlQuery html

摘要 git

函数 github

·                                 def addBindValue (val[, type=QSql.In]) sql

·                                 def at () 数据库

·                                 def bindValue (placeholder, val[, type=QSql.In]) ide

·                                 def bindValue (pos, val[, type=QSql.In]) 函数

·                                 def boundValue (placeholder) spa

·                                 def boundValue (pos) htm

·                                 def boundValues () ci

·                                 def clear ()

·                                 def driver ()

·                                 def execBatch ([mode=ValuesAsRows])

·                                 def exec_ ()

·                                 def exec_ (query)

·                                 def executedQuery ()

·                                 def finish ()

·                                 def first ()

·                                 def isActive ()

·                                 def isForwardOnly ()

·                                 def isNull (field)

·                                 def isSelect ()

·                                 def isValid ()

·                                 def last ()

·                                 def lastError ()

·                                 def lastInsertId ()

·                                 def lastQuery ()

·                                 def next ()

·                                 def nextResult ()

·                                 def numRowsAffected ()

·                                 def numericalPrecisionPolicy ()

·                                 def prepare (query)

·                                 def previous ()

·                                 def record ()

·                                 def result ()

·                                 def seek (i[, relative=false])

·                                 def setForwardOnly (forward)

·                                 def setNumericalPrecisionPolicy (precisionPolicy)

·                                 def size ()

·                                 def value (i)

细节描述

PySide.QtSql.QSqlQuery 提供执行SQL语句的方法。

PySide.QtSql.QSqlQuery 能够执行标准的DMLDDL,也能够执行专有的数据库语句。

若是成功执行SQL语句,设置query 的状态为活动的,因此PySide.QtSql.QSqlQuery.isActive()返回true,不然查询状态设为非活动的。

当执行一个新的SQL语句,query将固定在一个有效的记录位置,你能够使用如下函数进行导航:

·   PySide.QtSql.QSqlQuery.next()

·   PySide.QtSql.QSqlQuery.previous()

·   PySide.QtSql.QSqlQuery.first()

·   PySide.QtSql.QSqlQuery.last()

·   PySide.QtSql.QSqlQuery.seek()

这些函数提供了向前,向后 或任意查询返回记录的方法,你也能够用PySide.QtSql.QSqlQuery.setForwardOnly()来设置仅向前查询记录。

获取记录内容能够使用 PySide.QtSql.QSqlQuery.value()方法。

例如:

query = QSqlQuery("SELECT country FROM artist")

while query.next():

    country = query.value(0)

    doSomething(country)

使用value(int)函数时,int表示字段的位置,起始值是0.

使用”select * from….”进行查询时,请注意字段位置所对应的int值,以避免发生顺序错误。

在有些请况想引用具体字段的值,能够使用PySide.QtSql.QSqlQuery.record(). PySide.QtSql.QSqlRecord.indexOf()函数来肯定int的值。

:

query = QSqlQuery("SELECT * FROM artist")

fieldNo = query.record().indexOf("country")

while query.next():

    country = query.value(fieldNo)

    doSomething(country)

PySide.QtSql.QSqlQuery支持占位符绑定参数

能够用 PySide.QtSql.QSqlQuery.numRowsAffected()PySide.QtSql.QSqlQuery.size() 来检测多少条数据受影响,和检索了多大的数据。

如下是占位符绑定的例子:

使用名字绑定:

query = QSqlQuery()

query.prepare("INSERT INTO person (id, forename, surname) "

              "VALUES (:id, :forename, :surname)")

query.bindValue(":id", 1001)

query.bindValue(":forename", "Bart")

query.bindValue(":surname", "Simpson")

query.exec_()

根据位置绑定:

query = QSqlQuery()

query.prepare("INSERT INTO person (id, forename, surname) "

              "VALUES (:id, :forename, :surname)")

query.bindValue(0, 1001)

query.bindValue(1, "Bart")

query.bindValue(2, "Simpson")

query.exec_()

使用占位符绑定值 (版本1):

query = QSqlQuery()

query.prepare("INSERT INTO person (id, forename, surname) "

              "VALUES (?, ?, ?)")

query.bindValue(0, 1001)

query.bindValue(1, "Bart")

query.bindValue(2, "Simpson")

query.exec_()

使用占位符绑定值 (版本2):

query = QSqlQuery()

query.prepare("INSERT INTO person (id, forename, surname) "

              "VALUES (?, ?, ?)")

query.addBindValue(1001)

query.addBindValue("Bart")

query.addBindValue("Simpson")

query.exec_()

使用存储过程绑定的例子:

 AsciiToInt() 是存储过程,经过参数传递绑定。

query = QSqlQuery()

query.prepare("CALL AsciiToInt(?, ?)")

query.bindValue(0, "A")

query.bindValue(1, 0, QSql.Out)

query.exec_()

i = query.boundValue(1) # i is 65

注,哪些未绑定参数的将保留其值。

存储过程使用return语句返回一个值或多个结果集。但须要数据库驱动支持。

警告:创建 PySide.QtSql.QSqlQuery前必须加载驱动并打开链接。同时这个链接必须保持打开,不然是一种未定义的行为。

相关文章
相关标签/搜索