
Originally Posted by
wysota
The funny thing is you don't have to store the connection anywhere because Qt will automatically pick up the default connection. The name lookup is hash-based so it yields almost no overhead (apart from using a mutex to lock the dictionary). You have to pass *db everywhere which is awkward. And you have to delete the instance of the object somewhere.
Ok. That's what I figured, but that overhead is to much for me. >>1 dbs and very frequent queries. I think having a hidden global db connection list, with named lookup could be regarded as equally awkward. But if thats the proper way to go, then so be it. And if you only have one db, I guess it's a very convenient way to go, too.

Originally Posted by
wysota
Besides, there is a rule of a thumb that implicitly shared classes are to be created on the stack. There is nothing wrong in keeping a pointer to a database - if you want to do it and you have a reason, do it, but please don't teach others to follow Passing pointers around is not a good C++ habit.
Allright!

Originally Posted by
Lesiok
Sorry
JohannesMunk but Yours solution is not working on PostgreSQL (and on another DB too I think). Before executing some query You must call
QSqlDatabase::open() method. This method connecting to the real database not to the server.
No.. You can either open the database manually, or let QT do that for you, when you first use it:
From the docu:
QSqlDatabase::database ( const QString & connectionName = QLatin1String( defaultConnection ), bool open = true )
Returns the database connection called connectionName. The database connection must have been previously added with addDatabase(). If open is true (the default) and the database connection is not already open it is opened now[/CODE]
But you can also open it directly, which gives you direct feedback, if your connection settings are ok.
I give a shot at a more complete and correct (not pointer optimized) solution:
// server [B]connection[/B] - as stated in the docu
// without db.setDatabaseName("customdb");
{
// Parameter to addDatabase specifies the db-server-type / driver
db.setHostName("acidalia");
db.setUserName("mojito");
db.setPassword("J0a1m8");
// you can check here if all settings work..
bool ok = db.open();
}
// [B]Setup[/B] the db and start using it somewhere after successfully connecting to the server..
{
// Get back the default database connection.
// Create a new query on it
bool sr;
// Create your database if it does not exist already
sr = query.exec("CREATE DATABASE IF NOT EXISTS "+dbname);
if (!sr
) {ShowError
(QObject::tr("Database Error")+" "+db.
lastError().
text(),
true);
}
// Start using that database..
sr = query.exec("USE "+dbname);
// Make sure the correct Table Setup is present in DB..
sr = query.exec("CREATE TABLE IF NOT EXISTS Entries (id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, "
"caption VARCHAR(255) UNICODE, propmask LONGBLOB, properties LONGBLOB) ENGINE=InnoDB");
if (!sr
) {ShowError
(QObject::tr("Database Error")+" "+db.
lastError().
text(),
true);
} ...
}
// somewhere you'll need:
{
db.close();
}
// server [B]connection[/B] - as stated in the docu
// without db.setDatabaseName("customdb");
{
// Parameter to addDatabase specifies the db-server-type / driver
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("acidalia");
db.setUserName("mojito");
db.setPassword("J0a1m8");
// you can check here if all settings work..
bool ok = db.open();
}
// [B]Setup[/B] the db and start using it somewhere after successfully connecting to the server..
{
// Get back the default database connection.
QSqlDatabase db = QSqlDatabase::database();
// Create a new query on it
QSqlQuery query(db);
bool sr;
// Create your database if it does not exist already
sr = query.exec("CREATE DATABASE IF NOT EXISTS "+dbname);
if (!sr) {ShowError(QObject::tr("Database Error")+" "+db.lastError().text(),true);}
// Start using that database..
sr = query.exec("USE "+dbname);
// Make sure the correct Table Setup is present in DB..
sr = query.exec("CREATE TABLE IF NOT EXISTS Entries (id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, "
"caption VARCHAR(255) UNICODE, propmask LONGBLOB, properties LONGBLOB) ENGINE=InnoDB");
if (!sr) {ShowError(QObject::tr("Database Error")+" "+db.lastError().text(),true);}
...
}
// somewhere you'll need:
{
QSqlDatabase db = QSqlDatabase::database();
db.close();
}
To copy to clipboard, switch view to plain text mode
if you need more than one connection, just pass a 2nd parameter to QSqlDatabase::addDatabase("QMYSQL","internal connection name"); and pass it to the following QSqlDatabase::database("internal connection name") calls aswell.
@codematic: Problem solved?
Joh
Bookmarks