Search code examples
c++encryptionsqlitewxwidgetsdynamic-linking

Sqlite database encryption with wxsqlite3 run time error


I downloaded the prebuilt binaries from here(wxSQLite3 3.5.9) and also I downloaded the sqlite3.h file version 3.21.0, I added the header file and .dll and .lib file to my project.

I copied the 32 bit version of dll and lib file, and copied them in my solution and also added the .lib file to Additional Dependencies in Linker->Input in project properties.

I created this sample application with C++:

#define SQLITE_HAS_CODEC
#include "sqlite3.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
    sqlite3* db;
    sqlite3_open("test1.db", &db);

    sqlite3_key(
        db, /* Database to be rekeyed */
        "test", sizeof("test") /* The key, and the length of the key in bytes */
    );

    std::string createQuery =
        "CREATE TABLE IF NOT EXISTS items (userid INTEGER PRIMARY KEY, ipaddr       TEXT, username TEXT, useradd TEXT, userphone INTEGER, age INTEGER, "
        "time TEXT NOT NULL DEFAULT (NOW()));";

    sqlite3_stmt* createStmt;
    std::cout << "Creating Table Statement" << endl;
    sqlite3_prepare(db, createQuery.c_str(), createQuery.size(), &createStmt, NULL);
    cout << "Stepping Table Statement" << endl;
    if (sqlite3_step(createStmt) != SQLITE_DONE) cout << "Didn't Create Table!" << endl;

    string insertQuery =
        "INSERT INTO items (time, ipaddr,username,useradd,userphone,age) VALUES('7:30', '192.187.27.55', 'vivekanand', 'kolkatta', '04456823948', 74);";
    // WORKS!
    sqlite3_stmt* insertStmt;
    cout << "Creating Insert Statement" << endl;
    sqlite3_prepare(db, insertQuery.c_str(), insertQuery.size(), &insertStmt, NULL);
    cout << "Stepping Insert Statement" << endl;
    if (sqlite3_step(insertStmt) != SQLITE_DONE) cout << "Didn't Insert Item!" << endl;

    return 0;
}

when I run this program I get this error at run time: Run time error

But If I comment this section of code

sqlite3_key(
            db, /* Database to be rekeyed */
            "test", sizeof("test") /* The key, and the length of the key in bytes */
        );

It works just fine, what am I doing wrong?


Solution

  • The C/C++ code seems to be alright. And if the test application could be compiled correctly, obviously a valid link library was used. However, the runtime error message indicates that the correct DLL was not loaded.

    This indicates that the pre-compiled SQLite DLL was not copied into the same directory where the executable of the test application resides, or it was not in the search path of the application. However, some SQLite DLL was found in the search path, but most likely an "official" SQLite DLL that does not include the encryption extension.

    Make sure that the SQLite DLL that includes the encryption extension is accessible by the test application.

    The pre-compiled binaries from the wxSQLite3 releases definitely include the entry points sqlite3_key and sqlite3_rekey.