简体   繁体   English

插入char *作为向量 <byte> 在sqlite中

[英]Insert char* as vector<byte> in sqlite

I am trying to insert a char* (which was read from a file) into SQLite Database as a byte array ( vector<byte> ) to avoid being viewed explicitly. 我试图将char* (从文件中读取)作为字节数组( vector<byte> )插入SQLite数据库,以避免被显式查看。

Below is my code to read the file and have it in buffer as byte array, 以下是我的代码,用于读取文件并将其作为字节数组存储在缓冲区中,

void DataBaseUtils::readFileContents(string filePath)
{
    ifstream file(filePath, std::ios::binary);
    if (!file) {
        cerr << "An error occurred opening the file\n";
        return;
    }
    file.seekg(0, ifstream::end);
    size = file.tellg();
    file.seekg(0);

    char *buffer = new char[size];
    file.read(buffer, size);

    vector<byte> vByteArray(reinterpret_cast<byte>(buffer), size);

    mBlobBuffer = (char*)&vByteArray;

    mCharBuffer = buffer;

}

Below is where I insert the data into my source code, 下面是我将数据插入源代码的位置,

    std::stringstream strmInsert("");

    strmInsert << "INSERT or IGNORE INTO SAMPLETABLE(NAME, CONTENTS) VALUES(" << "'" << getName() << "'," << '?' << ")";

    string sInsert = strmInsert.str();
    char *strInsert = &sInsert[0];
    char *query1 = strInsert;

    sqlite3_stmt *stmt = NULL;

    int rc = sqlite3_prepare_v2(dbfile, query1, -1, &stmt, NULL);

    if (rc != SQLITE_OK) {
        cerr << "prepare failed: " << sqlite3_errmsg(dbfile) << endl;
    }
    else {
        // SQLITE_STATIC because the statement is finalized
        // before the buffer is freed:
        rc = sqlite3_bind_blob(stmt, 1, mCharBuffer /*mBlobBuffer*/, size, SQLITE_STATIC);
        if (rc != SQLITE_OK) {
            cerr << "bind failed: " << sqlite3_errmsg(dbfile) << endl;
        }
        else {
            rc = sqlite3_step(stmt);
            if (rc != SQLITE_DONE)
                cerr << "execution failed: " << sqlite3_errmsg(dbfile) << endl;
        }
    }
    sqlite3_finalize(stmt);

In my sqlite3_bind_blob statement, if I use mCharBuffer , the query runs properly but if I use mBlobBuffer , application crashes sometimes and works sometimes, without any pattern. 在我的sqlite3_bind_blob语句中,如果我使用mCharBuffer ,则查询可以正常运行,但是如果我使用mBlobBuffer ,则应用程序有时会崩溃并且有时可以正常工作,而没有任何模式。

How do I resolve it and insert the file contents as a BLOB data into CONTENTS column. 我该如何解决并将文件内容作为BLOB数据插入到CONTENTS列中。

Sample Image to show BLOB data 显示BLOB数据的样本图像

Sample Image to show Plain text 示例图像以显示纯文本

Edit : Added images for clarity. 编辑:添加了图像以便清晰。 I'd like to have the data as BLOB as first image rather than plain text in second (ABCD..) 我想将BLOB数据作为第一张图片而不是第二张纯文本(ABCD ..)

It is not possible to obscure your text by storing the vector<byte> in the database because the vector<byte> object doees not contain your text. 通过将vector<byte>存储在数据库中不可能掩盖您的文本,因为vector<byte>对象并不包含您的文本。 Not directly. 不直接。

A vector<byte> is basically a few pointers (maybe 3) that point to your text and nothing else. vector<byte>基本上是一些指向您的文本的指针(可能为3),而没有其他指向。 Storing the vector simply stores those pointers. 存储向量仅存储那些指针。

The only way to obscure your text is to encrypt it. 遮盖文字的唯一方法是加密文字。

One problem with this code is you casting std::vector<byte> to a char* when setting mBlobBuffer . 此代码的一个问题是,在设置mBlobBuffer时,将std::vector<byte>转换为char* Even if the vector was a char* (which it isn't) it gets destroyed at the end of the function. 即使向量是char* (不是),它也会在函数末尾销毁。 So mBlobBuffer points to nothing useful. 因此, mBlobBuffer指向没有任何用处。

I would consider making mCharBuffer a vector and just using that. 我会考虑将mCharBuffer向量,然后使用它。 You avoid dynamic allocation that way. 这样可以避免动态分配。 Something like this: 像这样:

class DataBaseUtils
{
public:
    void readFileContents(string filePath);

private:
    // make this  std::vector
    std::vector<char> mCharBuffer;
};

void DataBaseUtils::readFileContents(string filePath)
{
    std::ifstream file(filePath, std::ios::binary);
    if (!file) {
        std::cerr << "An error occurred opening the file\n";
        return;
    }

    file.seekg(0, std::ios::end);
    mCharBuffer.resize(file.tellg()); // no need to store the size elsewhere
    file.seekg(0, std::ios::beg);

    if(!file.read(mCharBuffer.data(), mCharBuffer.size())) {
        std::cerr << "An error occurred reading the file\n";
        return;
    }

    // need to encrypt the contents of the vector here
}

Then, when you come to put it into the function: 然后,当您将其放入函数中时:

rc = sqlite3_bind_blob(stmt, 1, mCharBuffer.data(), mCharBuffer.size(), SQLITE_STATIC);

The size is recorded in the vector. 大小记录在向量中。

Also you don't record when an error happens,I would throw an exception rather than just printing the error. 另外,您不会记录错误发生的时间,我会抛出异常,而不仅仅是打印错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM