cout << "按id查找图书信息\n" << endl; cout << "请输入要查询的书籍ID:"; cin >> book_id; string query = "select * from books where books_id = " + to_string(book_id); if (mysql_query(&mysql, query.c_str())) { cout << "Error: Query[查询成功] fail." << endl; break; } res = mysql_store_result(&mysql); //查询结果集 row = mysql_fetch_row(res); cout << "编号: " << row[0] << " 书名: " << row[1] << " 当前数量: " << row[2] << " 价格:¥" << row[3] << " 作者:" << row[4] << endl; break; }将这段c++代码更改为根据作者来查询图书信息
时间: 2024-02-10 18:05:17 浏览: 59
可以将原来的查询语句中的 `books_id` 改为 `author`,即:
```
string query = "select * from books where author = '" + author_name + "'";
```
其中,`author_name` 是用户输入的作者名字。同时,需要将输出结果的语句中的 `row[0]` 改为 `row[4]`,即:
```
cout << "编号: " << row[0] << " 书名: " << row[1] << " 当前数量: " << row[2] << " 价格:¥" << row[3] << " 作者:" << row[4] << endl;
```
相关问题
#include <iostream> #include <cstring> using namespace std; class Book { public: string title; string author; string publisher; string date; Book(string t, string a,string p,string d) { title=t; author=a; publisher=p; date=d; } ~Book(){} }; class Library { public: Book book[100]; int n; Library(){n=0;} void AddBook(Bookb) { book[n++]=b; } void DeleteBook(string t) { for (int i=0;i<n;i++) { if(book[i]->title==t) { delete book[i]; book[i]=NULL; for(int j=i;j<n-1;j++) { book[j]=book[j+1]; } n--; } } cout<<"未找到题目为"<<t<<"的书籍"<<endl; } void ShowBook() { cout<<"书籍显示"<<endl; for(int i=0;i<n;i++) { cout<<book[i]->title<<" "<<book[i]->author<<" "<<book[i]->publisher<<" "<<book[i]->date<<endl; } } BookFindBook(string t) { for (int i=0;i<n;i++) { if(book[i]->title==t) { return book[i]; } } cout<<"未找到题目为"<<t<<"的书籍"<<endl; return NULL; } ~Library() { for(int i=0;i<n;i++) { delete book[i]; } } }; int main() { Library l; int input; string t,a,p,d; cout << "------------------菜单栏------------------" << endl; cout << "1.图书信息添加" << endl; cout << "2.图书信息删除" << endl; cout << "3.图书信息查看" << endl; cout << "4.图书信息查找" << endl; cout << "0.退出" << endl; while(1) { cout << "请选择功能" << endl; cin >> input; switch (input) { case 1: { cout<<"请输入添加的图书书名:"; cin>>t; cout<<"请输入添加的图书作者:"; cin>>a; cout<<"请输入添加的图书出版社:"; cin>>p; cout<<"请输入添加的图书出版日期:"; cin>>d; Bookb=new Book(t,a,p,d); l.AddBook(b); break; } case 2: { cout<<"请输入删除的图书书名:"; cin>>t; l.DeleteBook(t); break; } case 3: { l.ShowBook(); break; } case 4: { cout<<"请输入查找的图书书名:"; cin>>t; Book *b=l.FindBook(t); if(b!=NULL) { cout<<"查询结果为:"<<b->title<<" "<<b->author<<" "<<b->publisher<<" "<<b->date<<endl; } break; } case 0: { cout<<"已退出"<<endl; return 0; } } } }优化这段程序
这段程序可以进行如下的优化:
1. 对输入进行错误处理,避免输入非法字符导致程序崩溃。
2. 在 Library 类中添加一个私有成员变量 max_len,表示 book 数组的最大长度,避免数组越界的错误。
3. 在 AddBook 方法中,当 book 数组已满时需要进行扩容。
4. 在 DeleteBook 和 FindBook 方法中,可以使用引用来避免对对象进行拷贝,提高程序效率。
5. 修改 ShowBook 方法的输出格式,让输出更加美观。
下面是修改后的程序:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class Book {
public:
string title;
string author;
string publisher;
string date;
Book(string t, string a, string p, string d) {
title = t;
author = a;
publisher = p;
date = d;
}
~Book() {}
};
class Library {
private:
Book* book;
int n;
int max_len;
public:
Library() {
n = 0;
max_len = 100;
book = new Book[max_len];
}
void AddBook(Book& b) {
if (n >= max_len) {
max_len *= 2;
Book* new_book = new Book[max_len];
memcpy(new_book, book, n * sizeof(Book));
delete[] book;
book = new_book;
}
book[n++] = b;
}
void DeleteBook(string t) {
for (int i = 0; i < n; i++) {
if (book[i].title == t) {
for (int j = i; j < n - 1; j++) {
book[j] = book[j + 1];
}
n--;
return;
}
}
cout << "未找到题目为 " << t << " 的书籍" << endl;
}
void ShowBook() {
cout << "------------------书籍显示------------------" << endl;
cout << "书名\t\t作者\t\t出版社\t\t出版日期" << endl;
for (int i = 0; i < n; i++) {
cout << book[i].title << "\t\t" << book[i].author << "\t\t" << book[i].publisher << "\t\t" << book[i].date << endl;
}
}
Book& FindBook(string t) {
for (int i = 0; i < n; i++) {
if (book[i].title == t) {
return book[i];
}
}
cout << "未找到题目为 " << t << " 的书籍" << endl;
static Book null_book("", "", "", "");
return null_book;
}
~Library() {
if (book) {
delete[] book;
}
}
};
int main() {
Library l;
int input;
string t, a, p, d;
cout << "------------------菜单栏------------------" << endl;
cout << "1.图书信息添加" << endl;
cout << "2.图书信息删除" << endl;
cout << "3.图书信息查看" << endl;
cout << "4.图书信息查找" << endl;
cout << "0.退出" << endl;
while (1) {
cout << "请选择功能: ";
cin >> input;
switch (input) {
case 1: {
cout << "请输入添加的图书书名:";
cin >> t;
cout << "请输入添加的图书作者:";
cin >> a;
cout << "请输入添加的图书出版社:";
cin >> p;
cout << "请输入添加的图书出版日期:";
cin >> d;
Book b(t, a, p, d);
l.AddBook(b);
break;
}
case 2: {
cout << "请输入删除的图书书名:";
cin >> t;
l.DeleteBook(t);
break;
}
case 3: {
l.ShowBook();
break;
}
case 4: {
cout << "请输入查找的图书书名:";
cin >> t;
Book& b = l.FindBook(t);
if (b.title != "") {
cout << "查询结果为:" << b.title << "\t\t" << b.author << "\t\t" << b.publisher << "\t\t" << b.date << endl;
}
break;
}
case 0: {
cout << "已退出" << endl;
return 0;
}
default: {
cout << "输入错误,请重新选择功能" << endl;
break;
}
}
}
}
```
printf("|添加书籍|\n"); cout << "请输入ID:"; cin >> book_id; cout << "请输入书名:"; cin >> name; cout << "请输入数量:"; cin >> number; cout << "请输入价格:"; cin >> price; cout << "请输入作者:"; cin >> author; cout << "请输入类型:"; cin >> type; cout << "当前信息为---id=" << book_id << ",书名=" << name << ",数量=" << number << ",价格=" << price << ",作者=" << author << "类型=" << type << endl; sql = "insert into books values(" + to_string(book_id) + ",'《" + name + "》'," + to_string(number) + "," + to_string(price) + ",'" + author + "','" + type + "')"; int ret = mysql_query(&mysql, sql.c_str()); if (ret) { cout << "Error: Query[添加失败] fail." << endl; break; }详细解释下上方代码
这段代码是一个添加书籍信息的功能,主要是通过用户输入书籍的ID、书名、数量、价格、作者和类型,将这些信息插入到MySQL数据库的books表中。具体流程如下:
1. 打印"添加书籍"的提示信息。
2. 用户输入书籍的ID、书名、数量、价格、作者和类型,并通过cin语句获取这些信息。
3. 打印当前输入的书籍信息,包括ID、书名、数量、价格、作者和类型。
4. 根据用户输入的信息,构造一个SQL插入语句,将书籍信息插入到MySQL数据库的books表中。构造SQL语句的过程中,需要将一些数据类型转换为字符串类型,如to_string函数将整型转换为字符串类型,以便能够拼接到SQL语句中。
5. 调用mysql_query函数执行SQL插入语句,如果插入失败,则打印"Error: Query[添加失败] fail."的提示信息,并跳出循环。
总的来说,这段代码实现了一个简单的添加书籍信息的功能,并将信息保存到MySQL数据库中。
阅读全文