gpt4 book ai didi

mysql - Qt 中未找到驱动程序错误

转载 作者:行者123 更新时间:2023-11-29 22:57:36 24 4
gpt4 key购买 nike

我试图用 Qt 5.2.1 连接 mysql。我运行了一个程序,但出现错误

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7

我在终端上运行以下命令来解决问题。

sudo apt-get install libqt5sql5-mysql

但问题仍未解决。

代码如下。

#include <QtGui>
#include <QtSql/QSql>
#include <QTableWidget>
#include <QApplication>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QtSql/QSqlDriver>
int main(int argc,char* argv[])
{
QApplication app(argc,argv);
QTableWidget* table = new QTableWidget();
table->setWindowTitle("Connect to Mysql Database Example");

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
qDebug()<<"abc";
db.setHostName("192.168.11.3");
db.setDatabaseName("menudb");
db.setUserName("root");
db.setPassword("test");
if (!db.open())
{
QMessageBox::critical(0, QObject::tr("Database Error"),
db.lastError().text());
}

QSqlQuery query("SELECT * FROM test");

table->setColumnCount(query.record().count());
table->setRowCount(query.size());

int index=0;
while (query.next())
{
table->setItem(index,0,new QTableWidgetItem(query.value(0).toString()));
table->setItem(index,1,new QTableWidgetItem(query.value(1).toString()));
index++;
}

table->show();
qDebug()<<"charu";
return app.exec();
}

请帮我解决这个问题。

最佳答案

QSqlDatabase: QMYSQL driver not loaded    
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7

这意味着您有 Qt 的 MySql 插件驱动程序,但可能没有 MySql 客户端库 (libmysqlclient.so)。
因此,请确保库路径中有 libmysqlclient.so。要检查插件的依赖关系,请使用:
objdump -p/PathToQt/plugins/sqldrivers/libqsqlmysql.so |需要 grep

关于mysql - Qt 中未找到驱动程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28655300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com