Results 1 to 4 of 4

Thread: QSqlDriver/handle and subscribeToNotification

  1. #1
    Join Date
    Sep 2006
    Location
    Rio de Janeiro, Brazil
    Posts
    44
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default QSqlDriver/handle and subscribeToNotification

    Hi Friends,

    I am trying to create an event notifications from the database using PostgreSQL, but I am not understanding how to create this event.

    The code connection:

    Qt Code:
    1. #include <QSqlDatabase>
    2. #include <QSqlDriver>
    3. #include <QVariant>
    4.  
    5. extern "C" {
    6. #include <postgresql/libpq-fe.h>
    7. }
    8.  
    9. inline static bool PQ()
    10. {
    11. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    12.  
    13. db.setDatabaseName("e193");
    14. db.setHostName("localhost");
    15.  
    16. QVariant value = db.driver()->handle();
    17.  
    18. if (value.typeName() == "PGconn*")
    19. {
    20. PGconn *handle = *static_cast<PGconn **>(value.data());
    21.  
    22. if (handle != 0)
    23. {}
    24. }
    25.  
    26. return true;
    27. }
    To copy to clipboard, switch view to plain text mode 

    With the code above the connection is created without problems, as do more to trigger the event of the database?

    When use:

    Qt Code:
    1. db.driver()->subscribeToNotification("LISTEN notify");
    To copy to clipboard, switch view to plain text mode 

    Get the following error:

    QPSQLDriver::subscribeToNotificationImplementation : database not open.

    More connection was opened! Please, someone could help me?

    Using Qt SnapShot -> Qt-4.4.0-snapshot-20071205

    Thank you, edm.

  2. #2
    Join Date
    Oct 2006
    Posts
    42
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlDriver/handle and subscribeToNotification

    Why are you getting the handle to the driver? You don't need it to use notifications.

    Are you really sure that the connection is open? I can't see any calls to open() in your code. Use isOpen() to see if the connection is really open or not before you call subscribeToNotification(). There are also a few problems in your code: subscribeToNotification() only takes the name of the event, thus you must remove LISTEN; and NOTIFY is a reserved word in PostgreSQL so you can't use it as an event id. My guess is that you want to do something like:

    Qt Code:
    1. // Already connected to the database somewhere else in your code
    2. QSqlDatabase db = QSqlDatabase::database();
    3. if (db.isOpen())
    4. db.driver()->subscribeToNotification("someEventId");
    To copy to clipboard, switch view to plain text mode 

    Note that the API is for subscribing to event notifications only. To actually trigger an event notification you'll have to use a query:
    Qt Code:
    1. q.exec("NOTIFY someEventId");
    To copy to clipboard, switch view to plain text mode 

    And to actually get the notification you have to connect to the notification signal.

  3. #3
    Join Date
    Sep 2006
    Location
    Rio de Janeiro, Brazil
    Posts
    44
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QSqlDriver/handle and subscribeToNotification

    Hummm ... Was doing wrong, now with its code above this worked, it was exactly what I wanted.

    But I am not able to connect the signal notification "QSqlDriver::notification", I am trying something like this:

    Qt Code:
    1. QObject::connect(db.driver(), SIGNAL(notification(nAlertTrigger)), this, SLOT(slotRefresh()));
    To copy to clipboard, switch view to plain text mode 

    How do I connect the event of notification?

    Thanks, edm.

  4. #4
    Join Date
    Oct 2006
    Posts
    42
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlDriver/handle and subscribeToNotification

    Qt Code:
    1. QObject::connect(db.driver(), SIGNAL(notification(const QString&)), this, SLOT(slotRefresh()));
    To copy to clipboard, switch view to plain text mode 

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

    ederbs (12th December 2007)

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.