PDA

View Full Version : Accessing sqlite3 handle causes exception



paulanon
5th February 2013, 21:22
I've been trying to access the sqlite3 handle and getting into an exception: "Unhandled exception at 0x778f15de in app.exe: 0xC0000005: Access violation."

Any idea? I've been following the following thread and somehow managed to not get the exception, but really not clear as to why (I've also asked on this thread):
http://www.qtcentre.org/threads/36131-Attempting-to-use-Sqlite-backup-api-from-driver-handle-fails?p=238095#post238095

And the code that causes the error:
Here's the code:



QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE", "testsqliteptr");
db.setDatabaseName("blabla.sqlite");
if (!db.open()) {
return -1;
}

v = db.driver()->handle();
sqlite3* sql3_db = *static_cast<sqlite3 **>(v.data());
//<tag_error>
sqlite3_exec(sql3_db, "CREATE TABLE if not exists ABC(foo,bar)", 0, 0, 0);


Thank you in advance.

wysota
5th February 2013, 21:54
First of all, maybe you should first check if the returned variant is valid? Second of all, you don't need a native handle to create a table in the database.

ChrisW67
6th February 2013, 00:19
You could take your error checking lead from the example code in the docs:


QSqlDatabase db = ...;
QVariant v = db.driver()->handle();
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*") == 0) {
// v.data() returns a pointer to the handle
sqlite3 *handle = *static_cast<sqlite3 **>(v.data());
if (handle != 0) { // check that it is not NULL
...
}
}


You may also come to grief because the Sqlite version the plugin is built against (maybe internal, maybe system supplied), and the version your application is built against are incompatible.

paulanon
6th February 2013, 05:21
First of all, maybe you should first check if the returned variant is valid? Second of all, you don't need a native handle to create a table in the database.
Sorry for my poor description and code. The values returned are valid. And I know this because the above code works when I first do sqlite3_backup_init() as suggested in this thread (http://www.qtcentre.org/threads/36131-Attempting-to-use-Sqlite-backup-api-from-driver-handle-fails?p=207558#post207558). I unfortunately need the native handle to work with an existing library/functions. Any help would be appreciated.

Added after 6 minutes:


You could take your error checking lead from the example code in the docs:


QSqlDatabase db = ...;
QVariant v = db.driver()->handle();
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*") == 0) {
// v.data() returns a pointer to the handle
sqlite3 *handle = *static_cast<sqlite3 **>(v.data());
if (handle != 0) { // check that it is not NULL
...
}
}


You may also come to grief because the Sqlite version the plugin is built against (maybe internal, maybe system supplied), and the version your application is built against are incompatible.
I should really tidy the code before asking here, but the values returned are valid. The code works without exception when I first call sqlite3_backup_init() as suggested in this thread (http://www.qtcentre.org/threads/36131-Attempting-to-use-Sqlite-backup-api-from-driver-handle-fails?p=207558#post207558). I am linking against the sqlite3.c file located in e.g. C:\Qt\4.8.4.sqlite\vs2008\src\3rdparty\sqlite. I presume the QtSql.dll (which is statically linked to the sqlite3 driver with vs2008) is compiled against the same splite3.c file. Feeling lost...