gpt4 book ai didi

c++ - 无法使用 sqlite3 和 C++ 解压缩从 sql 表中选择的压缩字符串

转载 作者:行者123 更新时间:2023-11-28 06:06:03 26 4
gpt4 key购买 nike

描述

我正在尝试将压缩数据插入到 sql 表中,然后提取并解压缩它。起初我压缩一个字符串,然后将它插入到一个表中(第 1 节)。压缩工作正常。即使当我在插入之前尝试解压缩它时,我也会取回字符串。问题出现在插入字符串并稍后尝试将其提取回来之后。

正如您在代码的第 2 部分中看到的那样。解压就不行了。我不知道为什么我从表中提取它后它不再起作用。


源代码

#include <zlc/zlibcomplete.hpp>
#include <string>
#include "3rdparty/sqlite_modern_cpp.h"

using namespace std;
using namespace zlibcomplete;

int main()
{
//SECTION 1
database db("data.db");
db << "CREATE TABLE IF NOT EXISTS paragraph(ID INTEGER PRIMARY KEY, LINE TEXT);";
string line = "hello world!";
//Compress string by using zlib
GZipCompressor compressor(9, auto_flush);
string compressed_line = compressor.compress(line);
compressor.finish();

//IF I DECOMPRESS STRING HERE IT WORKS FINE!!!
GZipDecompressor decompressor;
string output = decompressor.decompress(line);

//insert compressed string into table paragraph
db <<"insert into paragraph(ID,LINE) VALUES (?,?);"
<< 1
<< compressed_line;


//SECTION 2: Problem starts here where I try to return the compressed value
db <<"select line from paragraph where id = ?;"
<< "1"
>>[&](string line)
{
//Decompress string by using zlib
GZipDecompressor decompressor;
string output = decompressor.decompress(line);
//out is empty I don't know why???
cout << output << endl;
};
return 0;
}

修正后更新

第一次下载base64 solution 中指出的类.

#include <zlc/zlibcomplete.hpp>
#include <string>
#include "3rdparty/sqlite_modern_cpp.h"
#include "base64.h"

using namespace std;
using namespace zlibcomplete;

int main()
{
//SECTION 1
database db("data.db");
db << "CREATE TABLE IF NOT EXISTS paragraph(ID INTEGER PRIMARY KEY, LINE TEXT);";
string line = "hello world!";
//Compress string by using zlib
GZipCompressor compressor(9, auto_flush);
string compressed_line = compressor.compress(line);
compressor.finish();
std::string compressed = base64_encode(reinterpret_cast<const unsigned char*>(compressed_line.c_str()), compressed_line.length());

//insert compressed string into table paragraph
db <<"insert into paragraph(ID,LINE) VALUES (?,?);"
<< 1
<< compressed;


db <<"select line from paragraph where id = ?;"
<< "1"
>>[&](string line)
{
//decode HERE
std::string compressed_decoded = base64_decode(line);
//Decompress string by using zlib
GZipDecompressor decompressor;
string output = decompressor.decompress(line);
cout << output << endl;
};
return 0;
}

最佳答案

您的代码无法处理压缩字符串中的零。如果压缩字符串中有一个字符 0,那么文本将被截断并且无法再进行反向操作。

关于c++ - 无法使用 sqlite3 和 C++ 解压缩从 sql 表中选择的压缩字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32399447/

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