基于Qt和SQLite的图书管理系统 1、Qt中如何使用数据SQLite; 2、数据可增加、删除、修改、查询代码
时间: 2024-06-08 19:09:19 浏览: 155
1、在Qt中使用SQLite:
首先需要在Qt中添加SQLite的驱动程序,可以参考以下步骤:
1.1、下载SQLite驱动程序。可以从官网(https://www.sqlite.org/download.html)下载,或者从其他网站下载。
1.2、使用Qt自带的sqlite3.exe工具将SQLite库文件编译成动态链接库(.dll文件)。sqlite3.exe工具在Qt安装目录的bin目录下。
1.3、将编译好的SQLite动态链接库文件复制到Qt安装目录的plugins\sqldrivers目录下。注意,需要根据操作系统的不同,选择正确的库文件。
1.4、在Qt项目中添加对SQLite驱动程序的支持。在.pro文件中添加以下代码:
```
QT += sql
# 添加SQLite驱动程序
QT += sqlite
```
1.5、使用Qt提供的相关类(如QSqlDatabase、QSqlQuery等)进行数据库操作。
2、图书管理系统的增加、删除、修改、查询代码:
2.1、添加图书信息:
```cpp
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("bookstore.db");
if (!db.open()) {
QMessageBox::critical(nullptr, "Error", "Failed to open database.");
return;
}
QString name = ui->lineEdit_name->text();
QString author = ui->lineEdit_author->text();
QString publisher = ui->lineEdit_publisher->text();
QString isbn = ui->lineEdit_isbn->text();
QString price = ui->lineEdit_price->text();
QSqlQuery query;
query.prepare("INSERT INTO books (name, author, publisher, isbn, price) "
"VALUES (:name, :author, :publisher, :isbn, :price)");
query.bindValue(":name", name);
query.bindValue(":author", author);
query.bindValue(":publisher", publisher);
query.bindValue(":isbn", isbn);
query.bindValue(":price", price);
if (!query.exec()) {
QMessageBox::critical(nullptr, "Error", "Failed to add book.");
}
db.close();
```
2.2、删除图书信息:
```cpp
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("bookstore.db");
if (!db.open()) {
QMessageBox::critical(nullptr, "Error", "Failed to open database.");
return;
}
QString isbn = ui->lineEdit_isbn->text();
QSqlQuery query;
query.prepare("DELETE FROM books WHERE isbn = :isbn");
query.bindValue(":isbn", isbn);
if (!query.exec()) {
QMessageBox::critical(nullptr, "Error", "Failed to delete book.");
}
db.close();
```
2.3、修改图书信息:
```cpp
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("bookstore.db");
if (!db.open()) {
QMessageBox::critical(nullptr, "Error", "Failed to open database.");
return;
}
QString isbn = ui->lineEdit_isbn->text();
QString name = ui->lineEdit_name->text();
QString author = ui->lineEdit_author->text();
QString publisher = ui->lineEdit_publisher->text();
QString price = ui->lineEdit_price->text();
QSqlQuery query;
query.prepare("UPDATE books SET name = :name, author = :author, "
"publisher = :publisher, price = :price WHERE isbn = :isbn");
query.bindValue(":name", name);
query.bindValue(":author", author);
query.bindValue(":publisher", publisher);
query.bindValue(":price", price);
query.bindValue(":isbn", isbn);
if (!query.exec()) {
QMessageBox::critical(nullptr, "Error", "Failed to update book.");
}
db.close();
```
2.4、查询图书信息:
```cpp
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("bookstore.db");
if (!db.open()) {
QMessageBox::critical(nullptr, "Error", "Failed to open database.");
return;
}
QString keyword = ui->lineEdit_keyword->text();
QSqlQuery query;
query.prepare("SELECT * FROM books WHERE name LIKE :keyword OR "
"author LIKE :keyword OR publisher LIKE :keyword OR "
"isbn LIKE :keyword OR price LIKE :keyword");
query.bindValue(":keyword", "%" + keyword + "%");
if (!query.exec()) {
QMessageBox::critical(nullptr, "Error", "Failed to query books.");
}
ui->tableWidget->clearContents();
ui->tableWidget->setRowCount(0);
int row = 0;
while (query.next()) {
ui->tableWidget->insertRow(row);
ui->tableWidget->setItem(row, 0, new QTableWidgetItem(query.value(0).toString()));
ui->tableWidget->setItem(row, 1, new QTableWidgetItem(query.value(1).toString()));
ui->tableWidget->setItem(row, 2, new QTableWidgetItem(query.value(2).toString()));
ui->tableWidget->setItem(row, 3, new QTableWidgetItem(query.value(3).toString()));
ui->tableWidget->setItem(row, 4, new QTableWidgetItem(query.value(4).toString()));
row++;
}
db.close();
```
阅读全文