使用c连接器将字节数组写入mysql?

发布于 2024-12-10 17:49:00 字数 2991 浏览 0 评论 0原文

有没有什么聪明的方法可以将字节数组写入 varbinary 或(其他解释一下) 我希望制作这样的函数,

 bool writeBinary(char * someQuery, LPBYTE buffer)
 {
 }

当我使用下一个函数时,mysql中发生的数据与缓冲区不同

void CDBManager::SetBinary(const char * lpszStatement, LPBYTE lpBinaryBuffer,  
ULONG    BinaryBufferSize)
{
MYSQL_STMT  *stmt;
MYSQL_BIND  bind[1];
char * shhh = new char[BinaryBufferSize];
// ----
memcpy(shhh, lpBinaryBuffer, BinaryBufferSize);

stmt = mysql_stmt_init(&getDB()->m_hMySQL);
printf("\n");
if(BinaryBufferSize >= 2752)
{
    for(int i = 0; i < BinaryBufferSize; i++)
    {
        if(lpBinaryBuffer[i] == 0xFF &&
                 lpBinaryBuffer[i+1] == 0xFF && lpBinaryBuffer[i+2] == 0xFF && lpBinaryBuffer[i+3] == 0xFF && lpBinaryBuffer[i+4] == 0xFF && lpBinaryBuffer[i+5] == 0xFF &&
                  lpBinaryBuffer[i+6] == 0xFF && lpBinaryBuffer[i+7] == 0xFF && lpBinaryBuffer[i+8] == 0xFF && lpBinaryBuffer[i+9] == 0xFF && lpBinaryBuffer[i+10] == 0xFF &&
                   lpBinaryBuffer[i+11] == 0xFF && lpBinaryBuffer[i+12] == 0xFF && lpBinaryBuffer[i+13] == 0xFF && lpBinaryBuffer[i+14] == 0xFF && lpBinaryBuffer[i+15] == 0xFF)
        {
        }
        else
        {
            printf("[%d] %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", i, lpBinaryBuffer[i],
                 lpBinaryBuffer[i+1],  lpBinaryBuffer[i+2],  lpBinaryBuffer[i+3],  lpBinaryBuffer[i+4],  lpBinaryBuffer[i+5],
                  lpBinaryBuffer[i+6],  lpBinaryBuffer[i+7],  lpBinaryBuffer[i+8],  lpBinaryBuffer[i+9],  lpBinaryBuffer[i+10],
                   lpBinaryBuffer[i+11],  lpBinaryBuffer[i+12],  lpBinaryBuffer[i+13],  lpBinaryBuffer[i+14],  lpBinaryBuffer[i+15]);
        }
        i+=15;
    }
}
printf("\n");

if(stmt == NULL)
{
    fprintf(stderr, " mysql_stmt_init(), out of memory\n");
}
else if(mysql_stmt_prepare(stmt, lpszStatement, strlen(lpszStatement)))
{
    fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
    fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
}
else
{
    memset(bind, 0, sizeof(bind));
    // ----
    bind[0].buffer_type     = MYSQL_TYPE_BLOB;
    bind[0].buffer          = (char *)shhh;
    bind[0].buffer_length   = BinaryBufferSize;
    bind[0].is_null         = 0;
    bind[0].length          = 0;
    // ----
    // allways filed? what? this calls they are good, i made by example on mysql.com so what dont work
    // it always return len 0 => no data saved in db
    if(mysql_stmt_bind_param(stmt, bind))
    {
        fprintf(stderr, " mysql_stmt_bind_param() failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
    }
    else if(mysql_stmt_execute(stmt))
    {
        fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
    }
}
// ----
delete shhh;

}

谢谢

is there any smart way to write a byte array into varbinary or (other explain me)
im looking to make funciton like that

 bool writeBinary(char * someQuery, LPBYTE buffer)
 {
 }

when i using next function the data that happend in mysql not same to the buffer

void CDBManager::SetBinary(const char * lpszStatement, LPBYTE lpBinaryBuffer,  
ULONG    BinaryBufferSize)
{
MYSQL_STMT  *stmt;
MYSQL_BIND  bind[1];
char * shhh = new char[BinaryBufferSize];
// ----
memcpy(shhh, lpBinaryBuffer, BinaryBufferSize);

stmt = mysql_stmt_init(&getDB()->m_hMySQL);
printf("\n");
if(BinaryBufferSize >= 2752)
{
    for(int i = 0; i < BinaryBufferSize; i++)
    {
        if(lpBinaryBuffer[i] == 0xFF &&
                 lpBinaryBuffer[i+1] == 0xFF && lpBinaryBuffer[i+2] == 0xFF && lpBinaryBuffer[i+3] == 0xFF && lpBinaryBuffer[i+4] == 0xFF && lpBinaryBuffer[i+5] == 0xFF &&
                  lpBinaryBuffer[i+6] == 0xFF && lpBinaryBuffer[i+7] == 0xFF && lpBinaryBuffer[i+8] == 0xFF && lpBinaryBuffer[i+9] == 0xFF && lpBinaryBuffer[i+10] == 0xFF &&
                   lpBinaryBuffer[i+11] == 0xFF && lpBinaryBuffer[i+12] == 0xFF && lpBinaryBuffer[i+13] == 0xFF && lpBinaryBuffer[i+14] == 0xFF && lpBinaryBuffer[i+15] == 0xFF)
        {
        }
        else
        {
            printf("[%d] %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", i, lpBinaryBuffer[i],
                 lpBinaryBuffer[i+1],  lpBinaryBuffer[i+2],  lpBinaryBuffer[i+3],  lpBinaryBuffer[i+4],  lpBinaryBuffer[i+5],
                  lpBinaryBuffer[i+6],  lpBinaryBuffer[i+7],  lpBinaryBuffer[i+8],  lpBinaryBuffer[i+9],  lpBinaryBuffer[i+10],
                   lpBinaryBuffer[i+11],  lpBinaryBuffer[i+12],  lpBinaryBuffer[i+13],  lpBinaryBuffer[i+14],  lpBinaryBuffer[i+15]);
        }
        i+=15;
    }
}
printf("\n");

if(stmt == NULL)
{
    fprintf(stderr, " mysql_stmt_init(), out of memory\n");
}
else if(mysql_stmt_prepare(stmt, lpszStatement, strlen(lpszStatement)))
{
    fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
    fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
}
else
{
    memset(bind, 0, sizeof(bind));
    // ----
    bind[0].buffer_type     = MYSQL_TYPE_BLOB;
    bind[0].buffer          = (char *)shhh;
    bind[0].buffer_length   = BinaryBufferSize;
    bind[0].is_null         = 0;
    bind[0].length          = 0;
    // ----
    // allways filed? what? this calls they are good, i made by example on mysql.com so what dont work
    // it always return len 0 => no data saved in db
    if(mysql_stmt_bind_param(stmt, bind))
    {
        fprintf(stderr, " mysql_stmt_bind_param() failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
    }
    else if(mysql_stmt_execute(stmt))
    {
        fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
    }
}
// ----
delete shhh;

}

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新一帅帅 2024-12-17 17:49:00

在绑定和执行之间,添加以下行以将 blob 数据上传到服务器端:

*mysql_stmt_send_long_data({stmt}, {field-number}, {data}, {data length});*

if(mysql_stmt_send_long_data(stmt, 2, (const char*)ser.data(), data_length)) {
  ERROR("dehydrate : failed to send long data : %s", mysql_stmt_error(stmt));
  goto abort;
}

The field-number is从零开始,在您的情况下将为 0,因为您只有 1 字段。

此外,blob 类型仅支持最大 64k,您可能需要最大 1GB 的“long blob”数据类型。

Between your bind and your execute, add the following line to upload the blob data to the server side:

*mysql_stmt_send_long_data({stmt}, {field-number}, {data}, {data length});*

if(mysql_stmt_send_long_data(stmt, 2, (const char*)ser.data(), data_length)) {
  ERROR("dehydrate : failed to send long data : %s", mysql_stmt_error(stmt));
  goto abort;
}

The field-number is zero-based, and would be 0 in your case since you only have the 1 field.

Also, the blob type only supports up to 64k, you may want the "long blob" data type which is up to 1GB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文