Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kuldipsomwanshi opened this issue Jun 27, 2022 · 0 comments

Comments

@kuldipsomwanshi
Copy link

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();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant