简体   繁体   中英

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.

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.

How do I resolve it and insert the file contents as a BLOB data into CONTENTS column.

Sample Image to show BLOB data

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..)

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. Not directly.

A vector<byte> is basically a few pointers (maybe 3) that point to your text and nothing else. 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 . Even if the vector was a char* (which it isn't) it gets destroyed at the end of the function. So mBlobBuffer points to nothing useful.

I would consider making mCharBuffer a vector and just using that. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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