PDA

View Full Version : QSqlDriver/handle and subscribeToNotification



ederbs
10th December 2007, 14:24
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:



#include <QSqlDatabase>
#include <QSqlDriver>
#include <QVariant>

extern "C" {
#include <postgresql/libpq-fe.h>
}

inline static bool PQ()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");

db.setDatabaseName("e193");
db.setHostName("localhost");

QVariant value = db.driver()->handle();

if (value.typeName() == "PGconn*")
{
PGconn *handle = *static_cast<PGconn **>(value.data());

if (handle != 0)
{}
}

return true;
}



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

When use:



db.driver()->subscribeToNotification("LISTEN notify");


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.

mm78
10th December 2007, 16:12
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:


// Already connected to the database somewhere else in your code
QSqlDatabase db = QSqlDatabase::database();
if (db.isOpen())
db.driver()->subscribeToNotification("someEventId");


Note that the API is for subscribing to event notifications only. To actually trigger an event notification you'll have to use a query:


QSqlQuery q;
q.exec("NOTIFY someEventId");


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

ederbs
11th December 2007, 14:55
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:



QObject::connect(db.driver(), SIGNAL(notification(nAlertTrigger)), this, SLOT(slotRefresh()));


How do I connect the event of notification?

Thanks, edm.

mm78
11th December 2007, 15:40
QObject::connect(db.driver(), SIGNAL(notification(const QString&)), this, SLOT(slotRefresh()));