Results 1 to 11 of 11

Thread: how to captue sqlite's trigger generated err in QT?

  1. #1
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 47 Times in 43 Posts

    Question how to captue sqlite's trigger generated err in QT?

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    Default Re: how to captue sqlite's trigger generated err in QT?

    Hi you can try with
    Qt Code:
    1. if (SQLITE_CONSTRAINT == query.lastErro().number())
    2. {
    3. ... Your Code
    4. }
    To copy to clipboard, switch view to plain text mode 

    but I don't know if it works.
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 47 Times in 43 Posts

    Default Re: how to captue sqlite's trigger generated err in QT?

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    Default Re: how to captue sqlite's trigger generated err in QT?

    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.
    A camel can go 14 days without drink,
    I can't!!!

  5. The following user says thank you to mcosta for this useful post:

    BalaQT (29th October 2009)

  6. #5
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 47 Times in 43 Posts

    Question Re: how to captue sqlite's trigger generated err in QT?

    Any examples pls ? or alternate ways to achieve original task?

  7. #6
    Join Date
    Aug 2009
    Posts
    92
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: how to captue sqlite's trigger generated err in QT?

    Just do (for example):
    Qt Code:
    1. if ( ! query.exec("INSERT INTO bla bla bla ...") )
    2. qFatal("SQL Error <%s>: %s", qPrintable(query.lastQuery()), qPrintable(query.lastError().text()));
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    Default Re: how to captue sqlite's trigger generated err in QT?

    Example

    q is your QSqlQuery instance;
    Qt Code:
    1. if (!q.exec())
    2. {
    3. QVariant v = q.driver()->handle();
    4. if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*")==0) {
    5. // v.data() returns a pointer to the handle
    6. sqlite3 *handle = *static_cast<sqlite3 **>(v.data());
    7. if (handle != 0) { // check that it is not NULL
    8. qDebug("Error: %s", sqlite3_errmsg(handle));
    9. }
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  9. The following user says thank you to mcosta for this useful post:

    BalaQT (29th October 2009)

  10. #8
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 47 Times in 43 Posts

    Thumbs up Re: how to captue sqlite's trigger generated err in QT?

    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

  11. #9
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    Default Re: how to captue sqlite's trigger generated err in QT?

    you need to include sqlite3.h.
    A camel can go 14 days without drink,
    I can't!!!

  12. #10
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 47 Times in 43 Posts

    Default Re: how to captue sqlite's trigger generated err in QT?

    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

  13. #11
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 47 Times in 43 Posts

    Thumbs up [Solved] Re: how to captue sqlite's trigger generated err in QT?

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.