cout << "查询全部图书信息\n" << endl; printf("|查询书籍|\n"); //查询数据 成功返回0,查询失败返回随机值 if (mysql_query(&mysql, "select * from books;")) { cout << "Query[select] fail." << endl; break; } res = mysql_store_result(&mysql); //查询结果集 //根据结果集获取数据,如果不为空则打印 cout << " 编号" << " 书名" << " 当前数量 " << " 价格" << " 作者" << endl; while (row = mysql_fetch_row(res)) { //mysql_fetch_row:检索结果集的下一行 cout << " " << row[0] << " " << row[1] << " " << row[2] << " ¥" << row[3] << " " << row[4] << endl; } break;如和使这段c++打印输出各个数据可以居中对齐
时间: 2024-02-10 16:05:13 浏览: 28
可以使用`setw`和`setiosflags`来格式化输出,使各个数据居中对齐。具体实现如下:
```c++
cout << setiosflags(ios::left) << setw(10) << "编号" << setw(20) << "书名" << setw(20) << "当前数量" << setw(15) << "价格" << setw(15) << "作者" << endl;
while (row = mysql_fetch_row(res)) {
cout << setiosflags(ios::left) << setw(10) << row[0] << setw(20) << row[1] << setw(20) << row[2] << setw(15) << "¥" + string(row[3]) << setw(15) << row[4] << endl;
}
```
其中,`setiosflags(ios::left)`表示左对齐,`setw(n)`表示设置字段宽度为n个字符。需要注意的是,价格是一个字符串类型,需要先将其转换为string类型才能使用`setw`函数。
相关问题
case 3: { system("CLS"); cout << "———————————————————————————————————————————————————修改图书信息—————————————————————————————————————————————————————\n" << endl; printf("|修改书籍信息|\n"); //修改数据 成功返回1,查询失败返回0 cout << "请输入要修改的书籍ID:"; cin >> book_id; cout << "请输入变更的书名:"; cin >> name; cout << "请输入变更数量:"; cin >> number; cout << "请输入变更价格:"; cin >> price; cout << "请输入变更后作者:"; cin >> author; cout << "请输入变更后类型:"; cin >> type; sql = "update books set books_name ='《" + name + "》',books_number =" + to_string(number) + ",books_price =" + to_string(price) + ",books_author ='" + author + "',books_type ='" + type + "' where books_id = " + to_string(book_id); if (mysql_query(&mysql, sql.c_str())) { cout << "Error: Query[update] fail." << endl; break; } cout << "update success." << endl; cout << "——————————————————————————————————————————————————————————————————————————————————————————————————————————————————\n" << endl; break; }将这段c++代码改成可选择修改各个信息
可以通过添加一个菜单让用户选择修改哪个信息,具体修改代码如下所示:
case 3:
{
system("CLS");
cout << "———————————————————————————————————————————————————修改图书信息—————————————————————————————————————————————————————\n" << endl;
printf("|修改书籍信息|\n");
//修改数据 成功返回1,查询失败返回0
cout << "请输入要修改的书籍ID:";
cin >> book_id;
int choice;
cout << "请选择要修改的信息:" << endl;
cout << "1. 书名" << endl;
cout << "2. 数量" << endl;
cout << "3. 价格" << endl;
cout << "4. 作者" << endl;
cout << "5. 类型" << endl;
cin >> choice;
switch(choice) {
case 1:
cout << "请输入变更的书名:";
cin >> name;
sql = "update books set books_name ='《" + name + "》' where books_id = " + to_string(book_id);
break;
case 2:
cout << "请输入变更数量:";
cin >> number;
sql = "update books set books_number =" + to_string(number) + " where books_id = " + to_string(book_id);
break;
case 3:
cout << "请输入变更价格:";
cin >> price;
sql = "update books set books_price =" + to_string(price) + " where books_id = " + to_string(book_id);
break;
case 4:
cout << "请输入变更后作者:";
cin >> author;
sql = "update books set books_author ='" + author + "' where books_id = " + to_string(book_id);
break;
case 5:
cout << "请输入变更后类型:";
cin >> type;
sql = "update books set books_type ='" + type + "' where books_id = " + to_string(book_id);
break;
default:
cout << "输入错误!" << endl;
break;
}
if (mysql_query(&mysql, sql.c_str())) {
cout << "Error: Query[update] fail." << endl;
break;
}
cout << "update success." << endl;
cout << "——————————————————————————————————————————————————————————————————————————————————————————————————————————————————\n" << 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数据库中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)