PDA

View Full Version : MySQL connection problem



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

wysota
9th October 2007, 09:34
Check if you have MySQL Qt driver compiled and if you have MySQL client library installed on your system (it's called libmysqlclient).

myils
9th October 2007, 11:33
Hi wysota,
I installed Qt4 from the Ubuntu software repository using apt-get. I'm guessing that the MySQL Qt driver is OK since I am able to successfully connect to MySQL provided I do not specify a value for connectionName when calling QSqlDatabase::addDatabase().

I'm also guessing that the MySQL client library is installed on my system since I can find the following files:

/var/lib/dpkg/info/libmysqlclient15off.shlibs
/var/lib/dpkg/info/libmysqlclient15off.list
/var/lib/dpkg/info/libmysqlclient15off.postinst
/var/lib/dpkg/info/libmysqlclient15off.postrm
/var/lib/dpkg/info/libmysqlclient15off.md5sums
/var/lib/dpkg/info/libmysqlclient15-dev.list
/var/lib/dpkg/info/libmysqlclient15-dev.postrm
/usr/lib/libmysqlclient.so.15.0.0
/usr/lib/libmysqlclient_r.so.15.0.0
/usr/lib/libmysqlclient.so.15
/usr/lib/libmysqlclient_r.so.15
/usr/share/doc/libmysqlclient15off
/usr/share/doc/libmysqlclient15off/EXCEPTIONS-CLIENT.gz
/usr/share/doc/libmysqlclient15off/changelog.Debian.gz
/usr/share/doc/libmysqlclient15off/README.Debian
/usr/share/doc/libmysqlclient15off/copyright
/usr/share/doc/libmysqlclient15off/NEWS.Debian.gz

Thanks,
Myils

wysota
9th October 2007, 13:40
Maybe the location or user/password are invalid? Can you connect to the database passing the same data using mysql console (mysql -u root -p location)?

myils
9th October 2007, 21:21
The "location" table and user/password are valid. I know this because I can connect provided I execute the following:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

However I cannot connect to MySQL when I do this:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "someConnection");

wysota
9th October 2007, 21:37
Your query is surely invalid. You should associate it with the connection you created.

QSqlQuery query(QSqlDatabase::database("SomeName"));
query.exec("...");

myils
9th October 2007, 22:01
Thanks wysota,
Your last suggestion has fixed the problem :).
Myils