syedzia5
6th September 2012, 16:00
Hi,
I am trying to connect from Qt application(Windows) to MySQL(on the same m/c) with the following code but cannot connect:
qDebug() << QSqlDatabase::drivers();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setDatabaseName("test");
db.setHostName("localhost");
db.setPort(3306);
db.setUserName("root");
db.setPassword("zrsyed15967");
if (!db.open()) {
qDebug()<<db.lastError();
QMessageBox::critical(0, tr("Cannot open database"),
tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it."), QMessageBox::Cancel);
return;
}
This is the output I get:
("QSQLITE", "QMYSQL3", "QMYSQL", "QODBC3", "QODBC")
QSqlError(2003, "QMYSQL: Unable to connect", "Can't connect to MySQL server on 'localhost' (10061)")
================================================== ==============================
However when I try to connect to MySQL server via command line "mysql -u root -p -h localhost" it connects, here is the output:
Enter password: ***********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 5.5.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
================================================== =================
Kindly help :(
Zia
I am trying to connect from Qt application(Windows) to MySQL(on the same m/c) with the following code but cannot connect:
qDebug() << QSqlDatabase::drivers();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setDatabaseName("test");
db.setHostName("localhost");
db.setPort(3306);
db.setUserName("root");
db.setPassword("zrsyed15967");
if (!db.open()) {
qDebug()<<db.lastError();
QMessageBox::critical(0, tr("Cannot open database"),
tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it."), QMessageBox::Cancel);
return;
}
This is the output I get:
("QSQLITE", "QMYSQL3", "QMYSQL", "QODBC3", "QODBC")
QSqlError(2003, "QMYSQL: Unable to connect", "Can't connect to MySQL server on 'localhost' (10061)")
================================================== ==============================
However when I try to connect to MySQL server via command line "mysql -u root -p -h localhost" it connects, here is the output:
Enter password: ***********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 5.5.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
================================================== =================
Kindly help :(
Zia