尝试插入SQL Server数据库时'PK'错误附近的语法不正确

b

数据库异常– yii \ db \ Exception

Error Info: Array
(
    [0] => HY000
    [1] => 102
    [2] => Incorrect syntax near 'PK'. [102] (severity 15) [INSERT INTO [ORDERS] ([ID], [STATUS], [ZIPFILE], [COLOR], [PRICE]) VALUES (87, 1,'PK]
    [3] => -1
    [4] => 15
)

by原因:PDOException SQLSTATE [HY000]:常规错误:102'PK'附近的语法不正确。[102](严重性15)[插入[订单]([ID],[状态],[ZIPFILE],[颜色],[价格])值(87、1,'PK)

在yii2应用程序中,我试图使用file_get_uploads php函数上传zip文件,首先将zip转换为二进制文件,然后将其保存到SQL Server数据库的'ZIPFILE'列中,但收到上述错误。

看起来好像PK截断ZIPFILE的值以及SQL插入字符串中它后面的所有内容之后的特殊字符。如果我要通过使用PHPaddslashes()函数来转义特殊字符,则可以完成插入,但是zip文件由于附加的斜杠而损坏。更改编码不是一种选择,因为我使用另一个应用程序从数据库下载数据,并且该应用程序要求值采用此编码。

有没有一种方法可以转义特殊字符而不损坏zip文件?

我包含一些代码以使事情更清晰。

应用程序使用Yii2框架

控制器代码

public function actionCreate()
{
    $model = new Orders();
    $model->load(Yii::$app->request->post())
   
    //populates ZIPFILE property with binary value of the ASCII encoded ZIPFILE string(pre-populated) compressed to a .zip file and read using file_get_contents in the Zip model
    $model->ZIPFILE = (new Zip)->asBinaryString($model->ZIPFILE);
    
    if ($model->save()) {
        return $this->redirect(['view', 'id' => $model->ID]);
    } else {
        return $this->render(
            'create', [
                'model' => $model,
            ]);
    }
}

在邮编模型中

private string $_tempFolder = '/somecache/';

public function asBinaryString($content): ?string
    {
        $fileName = $this->create($content);
    
        $fileAsBinary = file_get_contents(Yii::$app->basePath . $this->_tempFolder . $fileName);
        
        return $fileAsBinary;   
    }

public function create($content): ?string
    {
        $zipName = \microtime();
        try {
            $zip = new \ZipArchive;
            if ($zip->open(Yii::$app->basePath . $this->_tempFolder . $zipName, \ZipArchive::CREATE) === true) {
                $zip->addFromString(time().'.RTF', $content);
                $zip->close();
            } else {
                throw new Exception(Yii::t('app', 'Archive could not be created in cache folder.'));
            }
        } catch (\Throwable $th) {
            throw $th; 
        } 
        return $zipName ?? null;
        
    }

加载回$ model-> ZIPFILE的字符串如下所示:“PKa PM?ٟ1590409143。RTFu n 0 _e`Os R j * ƄA 5Dݨʻ@ 0 ...''

无法通过数据库中的链接将文件存储到磁盘上,因为这是对旧版应用程序的增强,该应用程序要求将数据以zip格式存储在数据库中。

尝试通过使用PHP bin2hex()将二进制zip转换为hex来接近解决方案,但未成功。十六进制没有特殊字符;因此,$ model保存得很好,但另一个应用程序无法读取保存到SQL Server的zip文件。

这不是一个简单的错字问题,已经尝试过在此处使用准备好的语句,但是不起作用。

b

在解决之前,我花了至少10天的时间在网上进行广泛的搜索并测试了各种解决方案。

上面问题的答案是不可能将二进制字符串直接保存到SQL Server数据库中。该文件需要作为hex字符串发送

解:

public function asBinary($content): ?string
{
    $fileName = $this->create($content);
    $fileAsBinary = file_get_contents(Yii::$app->cache->cachePath . '/' . $fileName);
    return $fileAsBinary;   
}

/**
* zips file in cache folder into archive
*/
public function create($content): ?string
{
    $zipName = \microtime();
    try {
            $zip = new \ZipArchive;
            if ($zip->open(Yii::$app->cache->cachePath . '/'  . $zipName, \ZipArchive::CREATE) === true) {
                $zip->addFromString(time().'.RTF', $content);
                $zip->close();
            } else {
                throw new Exception(Yii::t('app', 'Archive could not be created in cache folder.'));
            }
    } catch (\Throwable $th) {
        throw $th;
    } 
    return $zipName ?? null;      
}

//return file as Hex string
public function asHex($content): string
{
    $fileAsBinary = $this->asBinary($content);
    $unpackedBinaryFile = unpack('H*hex', $fileAsBinary);
    $fileAsHex = '0x' . $unpackedBinaryFile['hex'];        
    return $fileAsHex;   
}

在$ model中填充ZIPFILE

$model->ZIPFILE = (new Zip)->asHex($model->ZIPFILE);

压缩文件需要转换为hex可以直接保存到SQL Server中字符串。VARBINARY当插入数据库时​​,SQL Server会将其转换为。

由于不清楚的原因,我无法使用Yii2的准备好的语句传递ZIPFILE。每次都会出错。另外,使用ActiveRecord的$model->save()方法无法保存必须使用SQL查询字符串插入。如果有人能说出原因,那就太好了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

SQL Server 2014的IIF``附近语法不正确''错误

来自分类Dev

SQL Server 错误:“'xyz' 附近的语法不正确”

来自分类Dev

sql server 上“=”错误附近的语法不正确

来自分类Dev

')'SQL SERVER附近的语法不正确

来自分类Dev

SQL Server:“@”附近的语法不正确

来自分类Dev

当我尝试向数据库中插入项目时,它在','附近显示不正确的语法?

来自分类Dev

更新 HTML 列时如何修复 T-SQL 错误“'Trebuchet' 附近的语法不正确”

来自分类Dev

SQL Server 2012:“按顺序求和”给出错误,“ order”附近的语法不正确

来自分类Dev

SQL Server错误:“修改”附近的语法不正确。在我的查询中

来自分类Dev

重新种子化的SQL Server语句抛出错误附近的语法不正确

来自分类Dev

使用动态SQL时附近的语法不正确

来自分类Dev

'JSON'附近的语法不正确-SQL Server 2016

来自分类Dev

SQL Server''。'附近的语法不正确的异常。'?

来自分类Dev

SQL SERVER sp_executesql'''附近的语法不正确

来自分类Dev

','附近的语法不正确。SQL Server和C#

来自分类Dev

SQL Server'11'附近的语法不正确

来自分类Dev

SQL Server 08')'附近的语法不正确

来自分类Dev

SQL Server“附近的语法不正确” WHERE语句

来自分类Dev

'0'附近的语法不正确。(Microsoft SQL Server)

来自分类Dev

'/' 附近的语法不正确。在 SQL Server 2012 中

来自分类Dev

[SQL Server]表附近语法不正确

来自分类Dev

尝试在T-sql中调用存储过程时'='附近的语法不正确

来自分类Dev

SQL Server的Microsoft OLE DB提供程序错误'80040e14''='附近的语法不正确

来自分类Dev

插入语句显示错误“ =”附近的语法不正确“

来自分类Dev

T-SQL错误:“ +”附近的语法不正确

来自分类Dev

SQL查询错误:“#”附近的语法不正确?

来自分类Dev

选择时语法不正确,名字附近的错误

来自分类Dev

从SQL Server 2005到2008时,“'索引'附近的语法不正确”

来自分类Dev

尝试在C#中保存存储过程时表附近语法不正确

Related 相关文章

  1. 1

    SQL Server 2014的IIF``附近语法不正确''错误

  2. 2

    SQL Server 错误:“'xyz' 附近的语法不正确”

  3. 3

    sql server 上“=”错误附近的语法不正确

  4. 4

    ')'SQL SERVER附近的语法不正确

  5. 5

    SQL Server:“@”附近的语法不正确

  6. 6

    当我尝试向数据库中插入项目时,它在','附近显示不正确的语法?

  7. 7

    更新 HTML 列时如何修复 T-SQL 错误“'Trebuchet' 附近的语法不正确”

  8. 8

    SQL Server 2012:“按顺序求和”给出错误,“ order”附近的语法不正确

  9. 9

    SQL Server错误:“修改”附近的语法不正确。在我的查询中

  10. 10

    重新种子化的SQL Server语句抛出错误附近的语法不正确

  11. 11

    使用动态SQL时附近的语法不正确

  12. 12

    'JSON'附近的语法不正确-SQL Server 2016

  13. 13

    SQL Server''。'附近的语法不正确的异常。'?

  14. 14

    SQL SERVER sp_executesql'''附近的语法不正确

  15. 15

    ','附近的语法不正确。SQL Server和C#

  16. 16

    SQL Server'11'附近的语法不正确

  17. 17

    SQL Server 08')'附近的语法不正确

  18. 18

    SQL Server“附近的语法不正确” WHERE语句

  19. 19

    '0'附近的语法不正确。(Microsoft SQL Server)

  20. 20

    '/' 附近的语法不正确。在 SQL Server 2012 中

  21. 21

    [SQL Server]表附近语法不正确

  22. 22

    尝试在T-sql中调用存储过程时'='附近的语法不正确

  23. 23

    SQL Server的Microsoft OLE DB提供程序错误'80040e14''='附近的语法不正确

  24. 24

    插入语句显示错误“ =”附近的语法不正确“

  25. 25

    T-SQL错误:“ +”附近的语法不正确

  26. 26

    SQL查询错误:“#”附近的语法不正确?

  27. 27

    选择时语法不正确,名字附近的错误

  28. 28

    从SQL Server 2005到2008时,“'索引'附近的语法不正确”

  29. 29

    尝试在C#中保存存储过程时表附近语法不正确

热门标签

归档