用C++和MySQL写一个订餐管理系统
时间: 2023-11-09 14:04:16 浏览: 76
好的,订餐管理系统主要涉及到数据的增删改查以及用户界面的交互,下面是一个简单的实现:
首先,需要安装MySQL数据库和C++的MySQL驱动库。
1. 创建数据库和表格
在MySQL中创建名为“order_system”的数据库,然后在其中创建名为“orders”的表格,表格包括以下字段:
- id: 订单ID,自增长整数
- customer_name: 客户姓名,字符串
- dish_name: 菜品名称,字符串
- price: 菜品价格,浮点数
- order_time: 下单时间,日期时间类型
可以使用以下命令创建表格:
```sql
CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
customer_name VARCHAR(255) NOT NULL,
dish_name VARCHAR(255) NOT NULL,
price FLOAT NOT NULL,
order_time DATETIME NOT NULL
);
```
2. C++代码实现
下面是一个简单的C++程序,实现了订餐管理系统的基本功能,包括添加订单、删除订单、查询订单和显示所有订单。
```cpp
#include <iostream>
#include <mysql.h>
using namespace std;
// MySQL连接信息
const char* host = "localhost";
const char* user = "root";
const char* password = "123456";
const char* database = "order_system";
// 连接到MySQL
MYSQL* connect_to_mysql() {
MYSQL* conn = mysql_init(NULL);
if (!mysql_real_connect(conn, host, user, password, database, 0, NULL, 0)) {
cerr << "Error connecting to database: " << mysql_error(conn) << endl;
return NULL;
}
return conn;
}
// 添加订单
void add_order(MYSQL* conn) {
string customer_name, dish_name;
float price;
cout << "Enter customer name: ";
getline(cin, customer_name);
cout << "Enter dish name: ";
getline(cin, dish_name);
cout << "Enter price: ";
cin >> price;
cin.ignore(); // 忽略换行符
string query = "INSERT INTO orders (customer_name, dish_name, price, order_time) VALUES ('"
+ customer_name + "', '"
+ dish_name + "', "
+ to_string(price) + ", NOW())";
if (mysql_query(conn, query.c_str())) {
cerr << "Error adding order: " << mysql_error(conn) << endl;
} else {
cout << "Order added successfully." << endl;
}
}
// 删除订单
void delete_order(MYSQL* conn) {
int id;
cout << "Enter order ID: ";
cin >> id;
cin.ignore(); // 忽略换行符
string query = "DELETE FROM orders WHERE id = " + to_string(id);
if (mysql_query(conn, query.c_str())) {
cerr << "Error deleting order: " << mysql_error(conn) << endl;
} else {
cout << "Order deleted successfully." << endl;
}
}
// 查询订单
void query_order(MYSQL* conn) {
int id;
cout << "Enter order ID: ";
cin >> id;
cin.ignore(); // 忽略换行符
string query = "SELECT * FROM orders WHERE id = " + to_string(id);
if (mysql_query(conn, query.c_str())) {
cerr << "Error querying order: " << mysql_error(conn) << endl;
} else {
MYSQL_RES* res = mysql_store_result(conn);
if (mysql_num_rows(res) == 0) {
cout << "Order not found." << endl;
} else {
MYSQL_ROW row = mysql_fetch_row(res);
cout << "ID: " << row[0] << endl;
cout << "Customer name: " << row[1] << endl;
cout << "Dish name: " << row[2] << endl;
cout << "Price: " << row[3] << endl;
cout << "Order time: " << row[4] << endl;
}
mysql_free_result(res);
}
}
// 显示所有订单
void display_all_orders(MYSQL* conn) {
string query = "SELECT * FROM orders";
if (mysql_query(conn, query.c_str())) {
cerr << "Error querying orders: " << mysql_error(conn) << endl;
} else {
MYSQL_RES* res = mysql_store_result(conn);
int num_rows = mysql_num_rows(res);
if (num_rows == 0) {
cout << "No orders found." << endl;
} else {
MYSQL_ROW row;
cout << "ID\tCustomer name\tDish name\tPrice\tOrder time" << endl;
while ((row = mysql_fetch_row(res))) {
cout << row[0] << "\t" << row[1] << "\t" << row[2] << "\t" << row[3] << "\t" << row[4] << endl;
}
}
mysql_free_result(res);
}
}
int main() {
MYSQL* conn = connect_to_mysql();
if (!conn) {
return 1;
}
while (true) {
cout << "1. Add order" << endl;
cout << "2. Delete order" << endl;
cout << "3. Query order" << endl;
cout << "4. Display all orders" << endl;
cout << "5. Quit" << endl;
int choice;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // 忽略换行符
switch (choice) {
case 1:
add_order(conn);
break;
case 2:
delete_order(conn);
break;
case 3:
query_order(conn);
break;
case 4:
display_all_orders(conn);
break;
case 5:
mysql_close(conn);
return 0;
default:
cout << "Invalid choice." << endl;
}
cout << endl;
}
}
```
程序中使用了MySQL C API提供的函数进行数据库操作。在主函数中,使用一个无限循环来接受用户输入,根据用户的选择调用相应的函数。
3. 编译和运行
将以上代码保存到一个名为“order_system.cpp”的文件中,然后使用以下命令编译:
```
g++ -o order_system order_system.cpp `mysql_config --cflags --libs`
```
运行程序:
```
./order_system
```
程序会提示用户输入选项,根据提示进行操作即可。
注意:以上代码仅为演示用途,实际应用中需要考虑更多的异常情况和安全性问题。
阅读全文