[solved] Unable to connect to MySql
-
wrote on 17 Apr 2012, 10:00 last edited by
I am trying connecting to mysql database on my computer. Here is what i tried:
@void SQL_Interface::on_pushButton_clicked()
{
dataBase = QSqlDatabase::addDatabase("QMYSQL");
dataBase.setHostName("root@localhost");
dataBase.setDatabaseName("testBase");
dataBase.setUserName("root");
dataBase.setPassword("xubuntu");
dataBase.setPort(3306);
//dataBase.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");bool ok = dataBase.open(); if(ok) { view = new QTableView; // doing something to ensure that connection was successfull view->show(); }
}@
Unfortunately, nothing happens. Here is what I do to start mysql from terminal:
@sudo myql -p@ -
wrote on 17 Apr 2012, 10:37 last edited by
Did you try to get any errors by using "QSqlDatabase::lastError()":http://qt-project.org/doc/qt-5.0/qsqldatabase.html#lastError? This might provide some hints about what's wrong. Try something like this:
@...
bool ok = dataBase.open();
if(ok)
{
...
}
else
{
qDebug() << "Error while opening DB connection: " << dataBase.lastError().text() << endl;
}@ -
wrote on 17 Apr 2012, 10:37 last edited by
First of all, take out the root@ from hostname, and make a little check on what happened at connection for example:
@
if(ok)
{
...
}else{
qDebug() << "SQL Error :" << database.lastError().text();
}
@And check what is the error.
-
wrote on 17 Apr 2012, 10:37 last edited by
upz, double answer ^_^
-
wrote on 17 Apr 2012, 10:49 last edited by
Its working fine now; i commented two statements:
@//dataBase.setHostName("root@localhost");
// dataBase.setDatabaseName("testBase"); @there was no dB by the name testBase in mysql
Thanks for your replies! -
wrote on 17 Apr 2012, 11:00 last edited by
we are glad to help :)
-
wrote on 17 Apr 2012, 15:49 last edited by
Please mark topics that are resolved as solved, not as closed. Threads can be closed by forum admins only and means that the comments are disabled. Thanks.
-
wrote on 17 Apr 2012, 16:28 last edited by
I will be careful next time. thanks!
-
wrote on 17 Apr 2012, 16:31 last edited by
No problem - every forum handles these things different. Once we have the "QnA features":/forums/viewforum/46/ live on the general forums, marking a thread as solved will be much easier.
1/9