Skip to content

Getting API misuse error (code 21)when trying to use SQLITE3 funtion sqlite3_prepare_v2 with QSQLCIPHER database #15

@kuldipsomwanshi

Description

@kuldipsomwanshi

I am creating the QSqlDatabe of QSQLCIPHER and then try to use sqlite3_prepare_v2 function but it is returning error code 21 that us bad parameter or API misuse. I am not able to understand that for QSQLITE the function working ok but not for QSQLCIPHER below is I am pasting my code

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLCIPHER");
auto handle = db.driver()->handle();
auto handle_data = const_cast<void*>(handle.data());
sqlite3* sqlite3_ptr = static_cast<sqlite3*>(handle_data);
sqlite3_stmt* prepared_stmt = nullptr;

auto execute_query = [sqlite3_ptr,
&prepared_stmt](const char* query) mutable -> int {
auto r =
sqlite3_prepare_v2(sqlite3_ptr, query, -1, &prepared_stmt, nullptr);

if (r != SQLITE_OK) return r;

};

if (!QFile::exists("test.db")) {
db.setDatabaseName("test.db");
if (!db.open()) {
QMessageBox::critical(
0,
tr("Cannot open database"),
tr("Unable to establish a database connection.\n"
"This example needs SQLCipher support."),
QMessageBox::Cancel);
return;
}

QSqlQuery query;
execute_query("pragma key = '12345';");
execute_query("create table person (id int primary key, "
              "name varchar(20), address varchar(200), typeid int)");
execute_query("insert into person values(1, 'Alice', "
              "'<qt>123 Main Street<br/>Market Town</qt>', 101)");
execute_query("insert into person values(2, 'Bob', "
              "'<qt>PO Box 32<br/>Mail Handling Service"
              "<br/>Service City</qt>', 102)");
execute_query("insert into person values(3, 'Carol', "
              "'<qt>The Lighthouse<br/>Remote Island</qt>', 103)");
execute_query("insert into person values(4, 'Donald', "
              "'<qt>47338 Park Avenue<br/>Big City</qt>', 101)");
execute_query("insert into person values(5, 'Emma', "
              "'<qt>Research Station<br/>Base Camp<br/>"
              "Big Mountain</qt>', 103)");
//! [Set up the main table]

//! [Set up the address type table]
execute_query(
    "create table addresstype (id int, description varchar(20))");
execute_query("insert into addresstype values(101, 'Home')");
execute_query("insert into addresstype values(102, 'Work')");
execute_query("insert into addresstype values(103, 'Other')");

}
else {
db.setDatabaseName("test.db");
if (!db.open()) {
QMessageBox::critical(
0,
tr("Cannot open database"),
tr("Unable to establish a database connection.\n"
"This example needs SQLCipher support."),
QMessageBox::Cancel);
return;
}

QSqlQuery query;
execute_query("pragma key = '12345';");

{ //===-Test-===
    execute_query("select * from addresstype;");
    if (query.lastError().type() != QSqlError::NoError) {
        QMessageBox::critical(
            0,
            tr("Cannot open database"),
            tr("%1").arg(query.lastError().text()),
            QMessageBox::Cancel);
        return;
    }
}

}

model = new QSqlRelationalTableModel(this);
model->setTable("person");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);

typeIndex = model->fieldIndex("typeid");

model->setRelation(
typeIndex, QSqlRelation("addresstype", "id", "description"));
model->select();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions