请给我用C++写一个图书管理系统,实现对图书的增删查改,并要求连接mysql8.0.31,有可视化界面操作
时间: 2024-02-25 09:56:12 浏览: 86
好的,以下是一个简单的C++图书管理系统,使用了MySQL C++ Connector来连接MySQL 8.0.31数据库,同时使用了Qt作为可视化界面,实现对图书的增删查改操作。
需要提前安装好MySQL C++ Connector和Qt开发环境,并在MySQL中创建好对应的数据库和表格。
```c++
#include <QApplication>
#include <QtSql>
#include <QTableView>
#include <QMessageBox>
#include <QSqlTableModel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 连接MySQL数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("library");
db.setUserName("root");
db.setPassword("password");
if (!db.open()) {
QMessageBox::critical(0, "Error", "Failed to connect to database.");
return 1;
}
// 创建TableModel
QSqlTableModel model;
model.setTable("book");
model.select();
// 创建TableView
QTableView *view = new QTableView;
view->setModel(&model);
// 创建增删查改操作的控件
QLabel *titleLabel = new QLabel("Title:");
QLineEdit *titleEdit = new QLineEdit;
QLabel *authorLabel = new QLabel("Author:");
QLineEdit *authorEdit = new QLineEdit;
QLabel *yearLabel = new QLabel("Year:");
QLineEdit *yearEdit = new QLineEdit;
QPushButton *addButton = new QPushButton("Add");
QPushButton *deleteButton = new QPushButton("Delete");
QPushButton *searchButton = new QPushButton("Search");
QPushButton *updateButton = new QPushButton("Update");
// 创建操作布局
QVBoxLayout *operationLayout = new QVBoxLayout;
operationLayout->addWidget(titleLabel);
operationLayout->addWidget(titleEdit);
operationLayout->addWidget(authorLabel);
operationLayout->addWidget(authorEdit);
operationLayout->addWidget(yearLabel);
operationLayout->addWidget(yearEdit);
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(addButton);
buttonLayout->addWidget(deleteButton);
buttonLayout->addWidget(searchButton);
buttonLayout->addWidget(updateButton);
operationLayout->addLayout(buttonLayout);
// 创建主布局
QWidget *widget = new QWidget;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(view);
layout->addLayout(operationLayout);
widget->setLayout(layout);
// 连接增删查改操作的信号和槽
QObject::connect(addButton, &QPushButton::clicked, [&]() {
QSqlRecord record = model.record();
record.setValue("title", titleEdit->text());
record.setValue("author", authorEdit->text());
record.setValue("year", yearEdit->text().toInt());
model.insertRecord(-1, record);
});
QObject::connect(deleteButton, &QPushButton::clicked, [&]() {
int row = view->currentIndex().row();
model.removeRow(row);
});
QObject::connect(searchButton, &QPushButton::clicked, [&]() {
QString title = titleEdit->text();
QString author = authorEdit->text();
int year = yearEdit->text().toInt();
QString filter = "";
if (!title.isEmpty())
filter += QString("title = '%1'").arg(title);
if (!author.isEmpty()) {
if (!filter.isEmpty())
filter += " and ";
filter += QString("author = '%1'").arg(author);
}
if (year != 0) {
if (!filter.isEmpty())
filter += " and ";
filter += QString("year = %1").arg(year);
}
model.setFilter(filter);
model.select();
});
QObject::connect(updateButton, &QPushButton::clicked, [&]() {
int row = view->currentIndex().row();
QSqlRecord record = model.record(row);
record.setValue("title", titleEdit->text());
record.setValue("author", authorEdit->text());
record.setValue("year", yearEdit->text().toInt());
model.setRecord(row, record);
});
// 显示窗口
widget->show();
return a.exec();
}
```
这个程序会连接名为"library"的MySQL数据库,其中有一张名为"book"的表格,表格中有三个字段:"title"、"author"和"year"。
界面上有一个TableView用来显示所有的图书信息,下面有四个操作控件,可以进行增删查改操作。在增加和修改操作中,需要输入图书的标题、作者和出版年份,然后点击Add或Update按钮即可完成操作。在删除操作中,需要先选中要删除的图书,然后点击Delete按钮即可完成操作。在查找操作中,可以输入图书的标题、作者和出版年份,然后点击Search按钮即可进行查找。
这只是一个简单的例子,实际的图书管理系统还需要考虑更多的功能和细节,比如借阅记录、图书分类、权限管理等等。
阅读全文