将大文本插入到BLOB中 [英] Inserting large text into blob

查看:94
本文介绍了将大文本插入到BLOB中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找这个问题的解决方案已经很久了,但在互联网上我都找不到答案。在这种情况下,我需要使用php插入或更新Firebird数据库中的BLOB字段(子类型1)。问题是,当文本变得非常大时,如果大于36k,它将不会执行查询。我知道行查询被限制为32k的数据,我尝试使用c#的参数化查询,但我无法在PHP中找到适合我的查询。离工作还远着呢。

我尝试了ibase_blob_Create等等,我尝试了直接插入,就像Insert into table (blob_value) values (?)ibase_prepare那样等等。对我来说似乎什么都不管用。有没有什么神奇的方法可以在php中实现这一点,或者从php中获取大文本到blob中是不可能的?

我试过使用以下内容:

class DBMgmt_Ibase_Blob extends DBMgmt_Generic_Blob
{
var $blob; // resourse link
var $id;
var $database;

function DBMgmt_Ibase_Blob(&$database, $id=null)
{
    $this->database =& $database;
    $this->id = $id;
    $this->blob = null;
}

function read($len)
{
    if ($this->id === false) return ''; // wr-only blob
    if (!($e=$this->_firstUse())) {
        return $e;
    }

    $data = @ibase_blob_get($this->blob, $len);
    if ($data === false) return $this->_setDbError('read');
    return $data;        
}

function write($data)
{
    if (!($e=$this->_firstUse())) return $e;
    $ok = @ibase_blob_add($this->blob, $data);
    if ($ok === false) return $this->_setDbError('add data to');
    return true;
}

function close()
{
    if (!($e=$this->_firstUse())) return $e;
    if ($this->blob) {
        $id = @ibase_blob_close($this->blob);
        if ($id === false) return $this->_setDbError('close');
        $this->blob = null;
    } else {
        $id = null;
    }
    return $this->id ? $this->id : $id;
}

function length()
{
    if ($this->id === false) return 0; // wr-only blob
    if (!($e=$this->_firstUse())) return $e;
    $info = @ibase_blob_info($this->id);
    if (!$info) return $this->_setDbError('get length of');
    return $info[0];
}

function _setDbError($query)
{
    $hId = $this->id === null ? "null" : ($this->id === false ? "false" : $this->id);
    $query = "-- $query BLOB $hId"; 
    $this->database->_setDbError($query);        
}

// Called on each blob use (reading or writing).
function _firstUse()
{
    // BLOB is opened - nothing to do.
    if (is_resource($this->blob)) return true;
    // Open or create blob.
    if ($this->id !== null) {
        $this->blob = @ibase_blob_open($this->id);
        if ($this->blob === false) return $this->_setDbError('open'); 
    } else {
        $this->blob = @ibase_blob_create($this->database->link);
        if ($this->blob === false) return $this->_setDbError('create');
    }
    return true;
}
}

我是这样使用的:

 $text = new DBMgmt_Ibase_Blob($DB);
            if (!$text->write(htmlspecialchars($Data["Text"]))){
                throw new Exception("blob fail");
            }
  $blobid = $text->close();
  //similar query
$DB->query("INSERT INTO TABLE1 (BLOB_VALUE) VALUES (?)",$blobid);

之后,我在数据库中找到一个我知道必须使用的数字:

Blob = new DBMgmt_Ibase_Blob($DB,$data->Text);
    $data->Text = $Blob->read($Blob->length());

但我得到的字符串不是数据库中带有数字(如30064771072)的Blob ID。

我尝试直接添加它,但当然不是这样工作的

$DB->query('insert into table (blob) values (?)',$string); // where string is like 170k chars

我收到以下错误:动态SQL错误SQL错误代码=-104命令行1的第236列意外结束

我尝试在php.net引用php.net blob_import之后将其放入文件中,代码类似于:

$dbh = ibase_connect($host, $username, $password);
$filename = '/tmp/bar';

 $fd = fopen($filename, 'r');
if ($fd) {

$blob = ibase_blob_import($dbh, $fd);
fclose($fd);

if (!is_string($blob)) {
    // import failed
} else {
    $query = "INSERT INTO foo (name, data) VALUES ('$filename', ?)";
    $prepared = ibase_prepare($dbh, $query);
    if (!ibase_execute($prepared, $blob)) {
        // record insertion failed
    }
}
} else {
// unable to open

但我仍获得类似Blob写入方法的结果。

推荐答案

您需要使用参数:

$query = ibase_prepare($DB, 'insert into table (blob) values (?)');
ibase_execute($query, $string);

cf:http://php.net/manual/en/function.ibase-execute.php

这篇关于将大文本插入到BLOB中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆