gpt4 book ai didi

c++ - 将 blob 从 mysql 复制到 postgres 进展不顺利(我再也看不到图像了)

转载 作者:行者123 更新时间:2023-11-30 01:32:06 25 4
gpt4 key购买 nike

我必须将 blob 图像(jpeg)从 mysql 数据库复制到带有字段 bytea 的 postgres 表。我使用 C++..我以这种方式从 mysql 读取图像

char* buffer = new char[res2->getInt(1)];
memset(buffer, '\0', res2->getInt(1));
(res->getBlob("att"))->read(buffer,res2->getInt(1));

res2 包含 LENGHT(blob_field)。现在,如果我将缓冲区保存在文件上,我可以完美地看到图像。之后我用

在 postgres 表上写入缓冲区
const char* const paramValues[] = {buffer};
const int nParams = sizeof(paramValues) / sizeof(paramValues[0]);
const int paramLenghts[] = {buffer_size };
const int paramFormats[] = {1 }; /*binary */


PQexecParams(
conn,
"INSERT INTO images (img) VALUES($1::bytea)",
nParams,
NULL, /* Types of parameters, unused as casts will define types */
paramValues,
paramLenghts,
paramFormats,
0
);

在postgres表中,我在bytea字段中有数据,但是如果我手动复制并粘贴到文件上,我看不到图像,因为它说不是jpeg图像...postgres字符串与mysql字符串不同(如果我用文本编辑器打开mysql)但我不明白为什么,因为我已经做了简单的复制和粘贴。有什么提示吗??

最佳答案

很可能是转义导致了您的问题。 Bytea 可以以文本或二进制模式返回。如果是文本模式,您将看到十六进制字符串或八进制转义字符串。在这种情况下,您需要首先通过 PQunescapeBytea 运行它。

如果这不是问题,那么下一步就是查看插入数据的方式。您可能错误地插入了它,因此数据库中的数据已损坏。

关于c++ - 将 blob 从 mysql 复制到 postgres 进展不顺利(我再也看不到图像了),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17324686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com