gpt4 book ai didi

c++ - qt+mysql交互问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:21:50 25 4
gpt4 key购买 nike

现在我正在做 Qt 项目,我需要一些 MySQL 支持。 MySQL 安装在 Ubuntu Server 的虚拟机(Virtual Box)上。这是我的做法:

int main(int argc, char** argv)
{
QApplication app(argc, argv);
QSqlDatabase hospital_db = QSqlDatabase::addDatabase("QMYSQL");
hospital_db.setHostName("192.168.56.10");
hospital_db.setDatabaseName("hospital_db");
hospital_db.setUserName("usr1");
hospital_db.setPassword("123");
if(!hospital_db.open()) {
qDebug() << "\nFailed connect to MySQL";
};

QTableView *tView = new QTableView;
QSqlTableModel model;

model.setTable("my_table");
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
if (!model.select()) {
QMessageBox msgBox;
QString err(model.lastError().text());
msgBox.setText(err);
msgBox.exec();
}
model.setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
model.setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
model.setHeaderData(2, Qt::Horizontal, QObject::tr("Address"));
model.setHeaderData(3, Qt::Horizontal, QObject::tr("Phone"));

tView->setModel(&model);
tView->show();

QSqlDatabase::removeDatabase("my_table");
return app.exec();
}

我收到“无法找到表 my_table”错误。表 my_table 已正确创建。这是 SHOW FIELDS OF my_table 的结果:

+----------------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-----------+------+-----+---------+----------------+
| hosp_id | int(11) | NO | PRI | NULL | auto_increment |
| hosp_name | char(100) | NO | | NULL | |
| hosp_address | char(100) | NO | | NULL | |
| hosp_ph_number | char(8) | NO | | NULL | |
+----------------+-----------+------+-----+---------+----------------+

最后是来自 MySQL 的 general_log:

| 2011-08-11 12:21:54 | usr1[usr1] @  [192.168.56.1] |        36 |         0 | Connect      | usr1@192.168.56.1 on hospital_db    |
| 2011-08-11 12:21:54 | usr1[usr1] @ [192.168.56.1] | 36 | 0 | Init DB | hospital_db |
| 2011-08-11 12:21:54 | usr1[usr1] @ [192.168.56.1] | 36 | 0 | Query | SET NAMES utf8 |
| 2011-08-11 12:21:54 | usr1[usr1] @ [192.168.56.1] | 36 | 0 | Field List | my_table |
| 2011-08-11 12:21:54 | usr1[usr1] @ [192.168.56.1] | 36 | 0 | Field List | my_table |
| 2011-08-11 12:21:54 | usr1[usr1] @ [192.168.56.1] | 36 | 0 | Query | show index from my_table |
| 2011-08-11 12:22:01 | usr1[usr1] @ [192.168.56.1] | 36 | 0 | Quit | |

那么,我做错了什么?

最佳答案

你在做什么

model.setTable("mytable");

但根据您的 SQL,表名是“my_table”。他们是不同的!

关于c++ - qt+mysql交互问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7029824/

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