Accessing MySQL database from C++
I want to connect to a MySQL database from a Qt C++ application. Database is located on a host and is available on internet. I have the MySQL username, password and IP of host and a port number (that I don't know what is)
Applicatio开发者_如何学Gon::getInstance()->db = QSqlDatabase::addDatabase("QMYSQL");
QSqlDatabase *db = &Application::getInstance()->db;
db->setHostName("64.34.119.12"); // The string is IP address of my host (not real)
db->setDatabaseName("foo");
db->setUserName("root");
db->setPassword("password");
db->open(); // returns false
I tried to pass IP address to db->setHostName()
but not worked: Connection fails.
Well, because you gave very little information, I cannot give you anything but a hunch, so here I go: Often webhosters configure there MySQL Databases to only accept connections from localhost (for a php or similar site, that is locate there, that's enough and that way it is more secure and abuse is less likely). That could be the reason why a connection from your machine is not accepted, though you have the right credentials.
精彩评论