Qt SQL - 配置与数据库的连接

时间:2013-01-20 06:36:53

标签: c++ sql qt qtsql

我已经放弃尝试为Qt 5.0库配置MYSQL驱动程序,我将使用目前唯一可用的驱动程序 - “QSQLITE”。

我一直试图让这个工作很长一段时间,并尝试过类似帖子中提到的所有内容: Select from SQLite with Qt

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setHostName(SQL_SERVER);
db.setPort(SQL_PORT);
db.setDatabaseName(SQL_DATABASE);
db.setUserName(SQL_USER);
db.setPassword(SQL_PASS);
bool dbSuccess = db.open();
QList<QString> deviceNames;
QString deviceName;
qDebug() << db;
if(dbSuccess){
    QSqlQuery query;
    qWarning("We made it into the DB");
    query.exec("SELECT device_name FROM tbl_device");
    while (query.next() ){
        qDebug() << query.value(1).toString();
       // deviceNames.append(deviceName);
        //qDebug() << "Test: "<< deviceName;
    }
}
else if(!db.open()){
    qWarning("Database failed to load!");
}

SQL_Server = 192.168.1.100

我从应用程序中获得以下qDebug输出:

QSqlDatabase(driver=""QSQLITE"", database=""homelogic"", host=""hendrenserver"", port=3306, user=""homelogic"",      open=true) 
We made it into the DB

输出表明数据库连接有效,但是如果我将servername更改为完全错误的东西,例如“xlkcjox”或其他随机密钥 - 我得到相同的输出。我在这里错过了什么?我觉得这应该相对容易。

请指教!

2 个答案:

答案 0 :(得分:2)

对qt使用sqlite驱动程序时,无论主机名是什么,数据库名称都是光盘上的文件。这是sqlite的工作原理。它不需要主机只是一个文件名。

答案 1 :(得分:0)

我正在重新审视这个问题,以分享我今天遇到的一个非常有用的链接。我使用Qt 4.8.4和QODBC驱动程序达成了解决方案。由于需要使用QSerialPort和项目错误,我今天更新到5.0.1。在重建我的ODBC插件时,我找到了这个链接:http://seppemagiels.com/blog/create-mysql-driver-qt5-windows。 在20分钟内,我得到了我原本想要的QMYSQL驱动程序。我希望这有助于其他人!