myils
9th October 2007, 00:59
Hi,
I'm using MySQL 5 and Qt 4.3 under Kubuntu 7.04.
I am able to open a connection to the database OK if I do the following:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setDatabaseName("location");
db.setUserName("root");
db.setPassword("blah");
if (!db.open()) {
cerr << "Database did not open successfully!" << endl;
return false;
}
However when I try and specify a unique connection name like the following, I am unable to connect:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "someConnection");
db.setDatabaseName("location");
db.setUserName("root");
db.setPassword("blah");
if (!db.open()) {
cerr << "Database did not open successfully!" << endl;
return false;
}
Does anyone know why I am unsuccessful in connecting in the second case?
Additional information:
In the second case the message "Database did not open successfully!" does not print. However when the first query is made the following error messages are received:
QSqlQuery::exec: database not open
Driver not loaded Driver not loaded
A full listing of the erroneous code is shown below:
#include <QApplication>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
//#include <QTextStream>
#include <iostream>
#include <QVariant>
using namespace std;
static bool createConnection()
{
QString title;
QSqlQuery query;
QString sql_string;
sql_string = "SELECT friend FROM friends WHERE username = \"Bob\"";
query.exec(sql_string);
if (!query.isActive())
std::cerr << qPrintable(query.lastError().text()) << std::endl;
while (query.next()) {
title = query.value(0).toString();
std::cerr << qPrintable(title) << endl;
}
return true;
}
int main()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "SomeName");
db.setDatabaseName("location");
db.setUserName("root");
db.setPassword("blah");
if (!db.open()) {
cerr << "Database did not open successfully!" << endl;
return false;
}
if (!createConnection())
return 1;
return 0;
}
Thanks,
Myils
I'm using MySQL 5 and Qt 4.3 under Kubuntu 7.04.
I am able to open a connection to the database OK if I do the following:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setDatabaseName("location");
db.setUserName("root");
db.setPassword("blah");
if (!db.open()) {
cerr << "Database did not open successfully!" << endl;
return false;
}
However when I try and specify a unique connection name like the following, I am unable to connect:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "someConnection");
db.setDatabaseName("location");
db.setUserName("root");
db.setPassword("blah");
if (!db.open()) {
cerr << "Database did not open successfully!" << endl;
return false;
}
Does anyone know why I am unsuccessful in connecting in the second case?
Additional information:
In the second case the message "Database did not open successfully!" does not print. However when the first query is made the following error messages are received:
QSqlQuery::exec: database not open
Driver not loaded Driver not loaded
A full listing of the erroneous code is shown below:
#include <QApplication>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
//#include <QTextStream>
#include <iostream>
#include <QVariant>
using namespace std;
static bool createConnection()
{
QString title;
QSqlQuery query;
QString sql_string;
sql_string = "SELECT friend FROM friends WHERE username = \"Bob\"";
query.exec(sql_string);
if (!query.isActive())
std::cerr << qPrintable(query.lastError().text()) << std::endl;
while (query.next()) {
title = query.value(0).toString();
std::cerr << qPrintable(title) << endl;
}
return true;
}
int main()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "SomeName");
db.setDatabaseName("location");
db.setUserName("root");
db.setPassword("blah");
if (!db.open()) {
cerr << "Database did not open successfully!" << endl;
return false;
}
if (!createConnection())
return 1;
return 0;
}
Thanks,
Myils