开发者

How to connect a QSqlDatabase using TCP? (Qt's MySQL Driver)

I am trying to connect to a local MySQL server, but cannot find out how to select if QSqlDatabase should connect through a socket or port. Qt is version 4.6.3. My simple test code is开发者_如何转开发 as follows:

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setDatabaseName("rem");
db.setUserName("test");
db.setPassword("test");

if (!db.open()) {
    qDebug() << db.lastError();
}

This gives me:

QSqlError(2002, "QMYSQL: Unable to connect",
    "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

As you can see it wants to connect using the default socket, which I don't want.

I found db.setConnectOptions("UNIX_SOCKET=blabla"), with which you can set the socket. Unfortunately I cannot figure out how to unset it.


use db.setHostName("127.0.0.1"); instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜