赞
踩
数据库的链接网上一大堆,在这里就不细讲了,注意的是include文件夹和lib文件夹要放到,程序的目录里。
实验内容如下:
按照软件工程的规范,设计并实现一个民航票务管理系统。要求该系统具有票务管
理(录入、修改、删除各航班机票信息)的功能和查询各航班信息的功能,其中票务管理
功能只能由管理员进行。需录入的信息包括(不限于):航班号、起点、终点、日期、
起飞时刻、到达时刻、票价、折扣票数、剩余座位数、航班所属航空公司。系统界面
要求美观、简捷,操作结果要准确。
我们需要在mysql里准备2个表格和内容
- create table flight_information(
-
- id int primary key,
-
- f_id varchar(10), -- 航班号
-
- f_start varchar(10), -- 起点
-
- f_end varchar(10), -- 终点
-
- f_date date, -- 日期
-
- f_off_time time, -- 起飞时刻
-
- f_reach_time time, -- 到达时刻
-
- f_price double, -- 票价
-
- f_discount_num int, -- 折扣票数
-
- f_remaining_seat int, -- 剩余座位数
-
- f_name varchar(10) -- 航班所属航空公司
-
- );
-
- create table admin(
-
- user varchar(10),
-
- password varchar(10)
-
- );
数据表格建议准备2个其中一个进行测试,另一个进行最后的测试
- #include <stdio.h>
- #include <iostream>
- #include "mysql.h"
- #include<string>
- using namespace std;
-
- class mysql_f
- {
- public:
- MYSQL mysql;
- MYSQL_RES* res;
- MYSQL_ROW row;
- int id, f_discount_num, f_remaining_seat;
- string f_id, f_start, f_end, f_date, f_off_time, f_reach_time, f_name, sql;
- double f_price;
-
- /*
- id int primary key,
- f_id varchar(20), -- 航班号
- f_start varchar(10), -- 起点
- f_end varchar(10), -- 终点
- f_date date, -- 日期
- f_off_time time, -- 起飞时刻
- f_reach_time time, -- 到达时刻
- f_price double, -- 票价
- f_discount_num int, -- 折扣票数
- f_remaining_seat int, -- 剩余座位数
- f_name varchar(10) -- 航班所属航空公司
- */
- void join_mysql()//链接数据库
- {
- mysql_init(&mysql);
- mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk");
- //第二个参数为主机地址localhost,第三个参数为用户名
- //第四个参数为用户密码,第五个参数为连接的数据库
- //第六个参数为MySQL的端口号3306
- if (mysql_real_connect(&mysql, "localhost", "root", "568301","test_03", 3306, NULL, 0) == NULL)
- {
- cout << (mysql_error(&mysql));
- }
- }
-
- void date_init()
- {
- cout << "请输入主键id" << endl;
- cin >> id;
- cout << "请输入航班号" << endl;
- cin >> f_id;
- cout << "请输入起点" << endl;
- cin >> f_start;
- cout << "请输入终点" << endl;
- cin >> f_end;
- cout << "请输入起飞日期" << endl;
- cin >> f_date;
- cout << "请输入起飞时刻" << endl;
- cin >> f_off_time;
- cout << "请输入到达时刻" << endl;
- cin >> f_reach_time;
- cout << "请输入票价" << endl;
- cin >> f_price;
- cout << "请输入折扣票数" << endl;
- cin >> f_discount_num;
- cout << "请输入剩余座位" << endl;
- cin >> f_remaining_seat;
- cout << "请输入所属航空公司" << endl;
- cin >> f_name;
-
- }
-
- void sql_init()
- {
- sql = "";
- sql += "(";
- sql += to_string(id);
- sql += ",";
- sql += f_id;
- sql += ",";
- sql += f_start;
- sql += ",";
- sql += f_end;
- sql += ",";
- sql += f_date;
- sql += ",";
- sql += f_off_time;
- sql += ",";
- sql += f_reach_time;
- sql += ",";
- sql += to_string(f_price);
- sql += ",";
- sql += to_string(f_discount_num);
- sql += ",";
- sql += to_string(f_remaining_seat);
- sql += ",";
- sql += f_name;
- sql += ");";
- }
-
- //查询数据库
- void inquire_mysql()
- {
- mysql_query(&mysql, "select * from test_03.flight_information");
- res = mysql_store_result(&mysql);
- //显示数据
- //给ROW赋值,判断ROW是否为空,不为空就打印数据。
- cout << "id\t" << "f_id\t" << "f_start\t" << "f_end\t\t" << "f_date\t\t" << "f_off_time\t" << "f_reach_time\t" << "f_price\t" << "f_remaining_seat\t" << "f_name\t" << endl;
- while (row = mysql_fetch_row(res))
- {
- printf("%s\t", row[0]);
- printf("%s\t", row[1]);
- printf("%s\t", row[2]);
- printf("%s\t", row[4]);
- printf("%s\t", row[5]);
- printf("%s\t", row[6]);
- printf("%s\t\t", row[7]);
- printf("%s\t\t", row[8]);
- printf("%s\t\t", row[9]);
- printf("%s\t", row[10]);
- cout << endl;
- }
- }
-
- bool authentication()//验证身份
- {
- string user, password, user1, password1;
- cout << "输入管理员账号:" << endl;
- cin >> user;
- cout << "输入密码:" << endl;
- cin >> password;
- mysql_query(&mysql, "select * from test_03.admin");
- res = mysql_store_result(&mysql);
- row = mysql_fetch_row(res);
- user1 = row[0];
- password1 = row[1];
- if (user1 == user && password == password1)
- {
- cout << "验证通过" << endl;
- }
- else
- {
- cout << "验证失败" << endl;
- return false;
- }
- return true;
- }
-
- void entering_f()//录入航班
- {
- if (authentication())
- {
- date_init();
- sql_init();
- sql = "insert into test_03.flight_information values" + sql;
- const char* char_sql = sql.data();
- if (mysql_query(&mysql, char_sql)) // 执行SQL语句
- {
- cout << "插入失败:" << mysql_error(&mysql) << endl;
- }
- else
- {
- cout << "插入成功..." << endl;
- }
- cout << "录入新航班之后的航班信息" << endl;
- inquire_mysql();
- }
-
- }
-
- void alter_f()//修改航班
- {
- if (authentication())
- {
-
- cout << "输入主键id" << endl;
- cin >> id;
- cout << "请输入起飞日期" << endl;
- cin >> f_date;
- cout << "请输入起飞时刻" << endl;
- cin >> f_off_time;
- cout << "请输入到达时刻" << endl;
- cin >> f_reach_time;
- cout << "请输入票价" << endl;
- cin >> f_price;
- cout << "请输入折扣票数" << endl;
- cin >> f_discount_num;
- cout << "请输入剩余座位" << endl;
- cin >> f_remaining_seat;
-
- sql += "f_date = ";
- sql += f_date;
- sql += ", f_off_time = ";
- sql += f_off_time;
- sql += ", f_reach_time = ";
- sql += f_reach_time;
- sql += ", f_price = ";
- sql += to_string(f_price);
- sql += ", f_discount_num = ";
- sql += to_string(f_discount_num);
- sql += ", f_remaining_seat = ";
- sql += to_string(f_remaining_seat);
- sql = "update flight_information set " + sql + " where id = " + to_string(id) + ";";
- const char* char_sql = sql.data();
- if (mysql_query(&mysql, char_sql)) // 执行SQL语句
- {
- cout << "修改失败:" << mysql_error(&mysql) << endl;
- }
- else
- {
- cout << "修改成功..." << endl;
- }
- cout << "修改之后的航班信息" << endl;
- inquire_mysql();
- }
-
- }
-
- void delete_f()//删除航班
- {
- if (authentication())
- {
- cout << "输入需要删除的主键id" << endl;
- cin >> id;
- sql += "delete from flight_information where id = " + to_string(id) + ";";
- const char* char_sql = sql.data();
- if (mysql_query(&mysql, char_sql)) // 执行SQL语句
- {
- cout << "删除失败:" << mysql_error(&mysql) << endl;
- }
- else
- {
- cout << "删除成功..." << endl;
-
- }
- cout << "删除之后的航班信息" << endl;
- inquire_mysql();
- }
-
- }
-
- void delete_mysql()//释放资源
- {
- //释放结果集
- mysql_free_result(res);
- //关闭数据库
- mysql_close(&mysql);
- }
-
- void menu()
- {
- cout << "无需登陆" << endl;
- cout << "1.查询航班信息" << endl;
- cout << "需要登陆" << endl;
- cout << "2.录入航班信息" << endl;
- cout << "3.修改航班信息" << endl;
- cout << "4.删除航班信息" << endl;
- cout << "0.退出" << endl;
- }
-
- };
-
- int main()
- {
- mysql_f ms;
- ms.join_mysql();
-
- int select = 0;
- cout << "请输入你要进行的操作" << endl;
- while (1)
- {
- ms.menu();
- cin >> select;
- switch (select)
- {
- case 1:
- ms.inquire_mysql();
- system("pause");
- system("cls");
- cout << "请输入你要进行的操作" << endl;
- break;
- case 2:
- ms.entering_f();
- system("pause");
- system("cls");
- cout << "请输入你要进行的操作" << endl;
- break;
- case 3:
- ms.alter_f();
- system("pause");
- system("cls");
- cout << "请输入你要进行的操作" << endl;
- break;
- case 4:
- ms.delete_f();
- system("pause");
- system("cls");
- cout << "请输入你要进行的操作" << endl;
- break;
- case 0:
- exit(0);
- break;
- default:
- break;
- }
- }
- ms.delete_mysql();
- system("pause");
- return 0;
- }
本次实验仍有需要不足的地方,还望读者,有自己的想法。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。