首页 > 解决方案 > Qt PL/SQL - 赋值运算符 - 字符串缓冲区太小

问题描述

我一直在想办法弄清楚这一点。

我遇到的问题是我在 Oracle 中使用的函数返回一个 BLOB。它是使用 || 连接在一起的项目列表。

根据我所做的研究,

  1. QSQLQuery 文档中,它说“不完全支持使用 return 语句返回值或返回多个结果集的存储过程。有关具体细节,请参阅 SQL 数据库驱动程序。” - 这让我相信如果 Qt 还不能处理它,我可能需要切换到不同的代码库。
  2. QOCI 驱动程序的文档提到“可以读写二进制大对象 (BLOB),但请注意,此过程可能需要大量内存。您应该使用仅向前查询来选择 LOB 字段(请参阅 QSqlQuery:: setForwardOnly())。”


query.setForwardOnly(true);
在我准备或执行语句之前,我确实设置了。

但是,我仍然收到此错误
QSqlError("6502", "Unable to execute statement", "ORA-06502: PL/SQL: numeric or value error: character string buffer too small\nORA-06512: at line 55\n")

我不得不稍微清理一下代码,我希望这仍然有助于为我要完成的工作提供上下文

              temp_clob       clob;
              name varchar2(183) := ?;
              start varchar2(16)  := ?;
              end   varchar2(16)  := ?;
              count  integer       := ?;
              return_val    named_redacted_table_object; -- Sorry had to remove this, it's a table with more Date, Varchar, etc
           begin
              dbms_lob.createtemporary(temp_clob, true);
              return_val := package_name.function_name (
                 set_name     => name,  
                 start_time   => to_date(start, 'yyyy-mm-dd hh24:mi'),                 
                 end_time     => to_date(end, 'yyyy-mm-dd hh24:mi'),
                 max_count    => count); 

        -- In here was a loop that would break apart the removed table object and make it into strings along the following lines
        -- '' || return_val(i).name || return_val(i).value || etc
        -- and would store these into the CLOB / temp_clob
              ? := temp_clob;
           end; 

我无法得到像这样简单的东西

begin
  ? := 'test123';
end; 

假设我至少可以在 Qt 中读取这个字符串。

这是我的 Qt 代码

QString name = "test";
QSqlQuery query(db);
query.setForwardOnly(true);
query.prepare(sql);
QString test_sql_text = ui->comboBox_test_text->currentText();
qDebug() << name;
query.bindValue(0, name);
query.bindValue(1, "2003-03-14 00:00");
query.bindValue(2, "2005-03-14 23:00");
query.bindValue(3, 2);
query.bindValue(4, QString(""), QSql::Out);
bool query_executed_ok = query.exec();
qDebug() << "did it execute?" << query_executed_ok;

// qDebug() << query.executedQuery();
qDebug() << query.boundValues();
qDebug() << query.lastError();
QSqlRecord rec = query.record();
qDebug() << rec;
int record_count = rec.count();
qDebug() << "Records: " << record_count;
while (query.next()) {
    for(int i=0;i<record_count;i++) {
       qDebug() << query.isValid() << " - " << rec.fieldName(i) << " " << query.value(i).toString();
    }
}

标签: c++sqloracleqtplsql

解决方案


begin
  ? := 'test123';
end; 

绑定变量用于为变量赋值。您在 pl/sql 代码中定义变量,并在运行时使用绑定变量为其赋值。在这种情况下,pl/sql 代码将正确编译。在您的代码中,绑定变量用于替换 pl/sql 变量,而不是值,这将失败。您的 pl/sql 块无法编译,因为它无法解析“?”。绑定变量的有效使用是

BEGIN
  l_xyz := ?;
END;

test123在运行时分配值的位置。


推荐阅读