关于Qt读取db2的中clob字段失败原因

来源:互联网 发布:mac口红vegas volt试色 编辑:程序博客网 时间:2024/06/02 16:27

 

    本人想实现在Qt中,通过数据库模块对db2中的clob,blob字段的存取,通过QSqlQuery::bindValue()存入clob数据是实现了

,但读取始终失败,执行exec()后,isActive()是成功的,但通过next()访问时,发现已经到了结尾,也就是没有取到一条记录

。如果用COUNT (*) FROM MyTable,还是会返回正确的记录数的。上网找了很多资料,他们都是通过next()成功获取clob字段的

,这样找了一天也一直不能解决。
   第二天把db2改成了mysql,发现读取是成功的。后来继续找资料,终于在http://doc.trolltech.com/4.5/sql-

driver.html#oci-blob-lob-support 上找到真相:
   BLOBs 可以读取和写入,但是注意这个过程可能需要占用大量的内存。你应当使用“只支持前身搜索的query”来选取LoB空

间(请看QSqlQuery::setForwardOnly());
  插入BLOBS 应当使用prepared query方式,它先将数据绑定到占位符上,或者QSqlTableModel中,后者其实也是采用prepared

query方式。

   Binary Large Objects (BLOBs) can be read and written, but be aware that this process may require a lot of

memory. You should use a forward only query to select LOB fields (see QSqlQuery::setForwardOnly()).

    Inserting BLOBs should be done using either a prepared query where the BLOBs are bound to placeholders or

QSqlTableModel, which uses a prepared query to do this internally.

原创粉丝点击