Hi!
Yeah.. the naming is a bit confusing. QSqlDatabase.. connects to a server.
You can create databases.. with queries:
query.exec("CREATE DATABASE IF NOT EXISTS "+dbname);
When you want to start using a specific database you do:
query.exec("USE "+dbname);
in code:
..
db->setPort(Port);
db->setHostName(hostname);
db->setUserName(username);
db->setPassword(password);
QSqlDatabase* db;
..
db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL",CDBIndex));
db->setPort(Port);
db->setHostName(hostname);
db->setUserName(username);
db->setPassword(password);
To copy to clipboard, switch view to plain text mode
Then the setup:
bool sr;
sr = query.exec("CREATE DATABASE IF NOT EXISTS "+dbname);
if (!sr
) {ShowError
(QObject::tr("Database Error")+" "+db
->lastError
().
text(),
true);
} sr = query.exec("USE "+dbname);
// Make sure the correct Table Setup is present in DB
// Entries: id, caption, propmask, properties
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);
}
QSqlQuery query(*db);
bool sr;
sr = query.exec("CREATE DATABASE IF NOT EXISTS "+dbname);
if (!sr) {ShowError(QObject::tr("Database Error")+" "+db->lastError().text(),true);}
sr = query.exec("USE "+dbname);
// Make sure the correct Table Setup is present in DB
// Entries: id, caption, propmask, properties
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);}
To copy to clipboard, switch view to plain text mode
Hope it helps! Good luck!
Joh
Bookmarks