PDA

View Full Version : How to get sqlite error message RAISE by trigger



miraks
17th November 2008, 15:54
Hi,

I have a sqlite database containing trigger to manage constraints.
(see attached file)

If I try to violate a constraint with sqlite3, the right error message is returned:

sqlite> delete from unit;
SQL error: delete on table unit violates foreign key constraint fkd_unit_unit_rc_unit_id_id :)

But, if I try to violate a constraint with QT API (QSqlQuery), this error message is returned by QSqlError::lastError():

constraint failed Impossible d'extraire la ligne :(

Do you know how to do to retrieve the right error message with QT API ?

In advance, thank you.

janus
17th November 2008, 17:06
Hi,

what about


QString QSqlError::driverText () const

or maybe you can use


int QSqlError::number () const

and get the sqlite error description from http://www.sqlite.org/c3ref/errcode.html

miraks
17th November 2008, 20:10
Hi Janus,

In fact, I am using QString QSqlError::text ().

In documentation, we can read:

This is a convenience function that returns databaseText() and driverText() concatenated into a single string.

So, driverText() is not the solution.

I don't know how to get the message RAISE by the trigger.

An other idea ?

janus
17th November 2008, 21:28
Hi,

maybe use the sqlite API directly? e.g. const char *sqlite3_errmsg(sqlite3*);
(http://www.sqlite.org/capi3ref.html#sqlite3_errcode)

from QSqlDriver :


This example retrieves the handle for a connection to sqlite:

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


... i used that for enabeling extension loading, but I can't find the code right now

EDit:
sry, i gave it a try ...guess my C/C++ knowledge is by far not good enought to use something like this. But using the handle to call a c sqlite function did work afair

miraks
22nd November 2008, 12:40
Thank you for the solution !