PDA

View Full Version : how to captue sqlite's trigger generated err in QT?



BalaQT
28th October 2009, 09:26
hi,
im using QT4.5 and sqlite3

im using BEFORE INSERT TRIGGER for validation purpose
[is it good idea to put validation on before_insert_trigger???]

im generating err , when the validation fails

like

create TRIGGER MobileValid BEFORE INSERT on PARAM_DETAILS
when new.PARAM_CODE=12
BEGIN
SELECT RAISE(ABORT,'First Digit Should Be 9') where substr(new.PARAM_VALUE,1,1)<>'9' ;
SELECT RAISE(ABORT,'Length Should Be 10') where length(new.PARAM_VALUE)<>10 ;
END;

This is working fine.

My question is , how to get this TRIGGER RAISE ERROR in QT application?


IN QT im inserting as
q.exec("insert into PARAM_DETAILS(PARAM_CODE,PARAM_VALUE) values(12,'9789')");

when i execute this insert code in sqlite3 cmd prompt , i got the error raise by trigger;

but how to capture this err in QT APPLICAITON?

pls guide me?

bala

mcosta
28th October 2009, 10:41
Hi you can try with


if (SQLITE_CONSTRAINT == query.lastErro().number())
{
... Your Code
}


but I don't know if it works.

BalaQT
28th October 2009, 11:09
thnks fr ur reply mcosta
its not working

I have tried q.lasterror();
QSqlError(19, "Unable to fetch row", "constraint failed")


but this is a general err. but i want the Trigger's Raise error text

'First Digit Should Be 9' like this

thnks
bala

mcosta
28th October 2009, 11:44
Qt use a "layer" for hide specific database features.

If you want use them, you have to access to sqlite API.

Try using QSqlDriver::handle () to access the sqlite3 type and use sqlite API.

BalaQT
28th October 2009, 13:39
Any examples pls ? or alternate ways to achieve original task?

trallallero
28th October 2009, 13:42
Just do (for example):

if ( ! query.exec("INSERT INTO bla bla bla ...") )
qFatal("SQL Error <%s>: %s", qPrintable(query.lastQuery()), qPrintable(query.lastError().text()));

mcosta
28th October 2009, 14:15
Example

q is your QSqlQuery instance;


if (!q.exec())
{
QVariant v = q.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
qDebug("Error: %s", sqlite3_errmsg(handle));
}
}
}

BalaQT
29th October 2009, 13:24
Great example mcosta,
Thnks for ur support.

I implemented ur example but getting following errors
"353: error: ‘sqlite3’ was not declared in this scope"
"356: error: ‘sqlite3_errmsg’ was not declared in this scope"

Which header i need to include , to avoid these errors.?

The first error was removed by adding #include <qsql_sqlite.h>
but after that, i got the second one "356: error: ‘sqlite3_errmsg’
was not declared in this scope"

pls guide me

Thnks
Bala

mcosta
29th October 2009, 15:22
you need to include sqlite3.h.

BalaQT
30th October 2009, 08:35
hi mcosta,
Thnks fr ur support. I have included sqlite3.h.
But getting the following err

69: undefined reference to`sqlite3_errmsg'

:-1: error: collect2: ld returned 1 exit status


Thnks
Bala

BalaQT
14th November 2009, 13:42
:D Problem Solved

Thnks mcosta

The problem is solved by adding
the following line to the .pro file

LIBS += -L/usr/lib -lsqlite3

and added #include "sqlite3.h"

Thnks for ur kind support

Bala