当前位置:   article > 正文

C++ + 接数据库 -> 实现机票查询,修改,更新,删除功能_机票删代码

机票删代码

数据库的链接网上一大堆,在这里就不细讲了,注意的是include文件夹和lib文件夹要放到,程序的目录里。

实验内容如下:

按照软件工程的规范,设计并实现一个民航票务管理系统。要求该系统具有票务管

理(录入、修改、删除各航班机票信息)的功能和查询各航班信息的功能,其中票务管理

功能只能由管理员进行。需录入的信息包括(不限于):航班号、起点、终点、日期、

起飞时刻、到达时刻、票价、折扣票数、剩余座位数、航班所属航空公司。系统界面

要求美观、简捷,操作结果要准确。

我们需要在mysql里准备2个表格和内容

  1. create table flight_information(
  2. id int primary key,
  3. f_id varchar(10),      -- 航班号
  4. f_start varchar(10),     -- 起点
  5. f_end varchar(10),       -- 终点
  6. f_date date,            -- 日期
  7. f_off_time time,       -- 起飞时刻
  8. f_reach_time time,         -- 到达时刻
  9. f_price double,          -- 票价
  10. f_discount_num int,        -- 折扣票数
  11. f_remaining_seat int,     -- 剩余座位数
  12. f_name varchar(10)        -- 航班所属航空公司
  13. );
  14. create table admin(
  15. user varchar(10),
  16. password varchar(10)
  17. );

数据表格建议准备2个其中一个进行测试,另一个进行最后的测试

 

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include "mysql.h"
  4. #include<string>
  5. using namespace std;
  6. class mysql_f
  7. {
  8. public:
  9. MYSQL mysql;
  10. MYSQL_RES* res;
  11. MYSQL_ROW row;
  12. int id, f_discount_num, f_remaining_seat;
  13. string f_id, f_start, f_end, f_date, f_off_time, f_reach_time, f_name, sql;
  14. double f_price;
  15. /*
  16. id int primary key,
  17. f_id varchar(20), -- 航班号
  18. f_start varchar(10), -- 起点
  19. f_end varchar(10), -- 终点
  20. f_date date, -- 日期
  21. f_off_time time, -- 起飞时刻
  22. f_reach_time time, -- 到达时刻
  23. f_price double, -- 票价
  24. f_discount_num int, -- 折扣票数
  25. f_remaining_seat int, -- 剩余座位数
  26. f_name varchar(10) -- 航班所属航空公司
  27. */
  28. void join_mysql()//链接数据库
  29. {
  30. mysql_init(&mysql);
  31. mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk");
  32. //第二个参数为主机地址localhost,第三个参数为用户名
  33. //第四个参数为用户密码,第五个参数为连接的数据库
  34. //第六个参数为MySQL的端口号3306
  35. if (mysql_real_connect(&mysql, "localhost", "root", "568301","test_03", 3306, NULL, 0) == NULL)
  36. {
  37. cout << (mysql_error(&mysql));
  38. }
  39. }
  40. void date_init()
  41. {
  42. cout << "请输入主键id" << endl;
  43. cin >> id;
  44. cout << "请输入航班号" << endl;
  45. cin >> f_id;
  46. cout << "请输入起点" << endl;
  47. cin >> f_start;
  48. cout << "请输入终点" << endl;
  49. cin >> f_end;
  50. cout << "请输入起飞日期" << endl;
  51. cin >> f_date;
  52. cout << "请输入起飞时刻" << endl;
  53. cin >> f_off_time;
  54. cout << "请输入到达时刻" << endl;
  55. cin >> f_reach_time;
  56. cout << "请输入票价" << endl;
  57. cin >> f_price;
  58. cout << "请输入折扣票数" << endl;
  59. cin >> f_discount_num;
  60. cout << "请输入剩余座位" << endl;
  61. cin >> f_remaining_seat;
  62. cout << "请输入所属航空公司" << endl;
  63. cin >> f_name;
  64. }
  65. void sql_init()
  66. {
  67. sql = "";
  68. sql += "(";
  69. sql += to_string(id);
  70. sql += ",";
  71. sql += f_id;
  72. sql += ",";
  73. sql += f_start;
  74. sql += ",";
  75. sql += f_end;
  76. sql += ",";
  77. sql += f_date;
  78. sql += ",";
  79. sql += f_off_time;
  80. sql += ",";
  81. sql += f_reach_time;
  82. sql += ",";
  83. sql += to_string(f_price);
  84. sql += ",";
  85. sql += to_string(f_discount_num);
  86. sql += ",";
  87. sql += to_string(f_remaining_seat);
  88. sql += ",";
  89. sql += f_name;
  90. sql += ");";
  91. }
  92. //查询数据库
  93. void inquire_mysql()
  94. {
  95. mysql_query(&mysql, "select * from test_03.flight_information");
  96. res = mysql_store_result(&mysql);
  97. //显示数据
  98. //给ROW赋值,判断ROW是否为空,不为空就打印数据。
  99. 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;
  100. while (row = mysql_fetch_row(res))
  101. {
  102. printf("%s\t", row[0]);
  103. printf("%s\t", row[1]);
  104. printf("%s\t", row[2]);
  105. printf("%s\t", row[4]);
  106. printf("%s\t", row[5]);
  107. printf("%s\t", row[6]);
  108. printf("%s\t\t", row[7]);
  109. printf("%s\t\t", row[8]);
  110. printf("%s\t\t", row[9]);
  111. printf("%s\t", row[10]);
  112. cout << endl;
  113. }
  114. }
  115. bool authentication()//验证身份
  116. {
  117. string user, password, user1, password1;
  118. cout << "输入管理员账号:" << endl;
  119. cin >> user;
  120. cout << "输入密码:" << endl;
  121. cin >> password;
  122. mysql_query(&mysql, "select * from test_03.admin");
  123. res = mysql_store_result(&mysql);
  124. row = mysql_fetch_row(res);
  125. user1 = row[0];
  126. password1 = row[1];
  127. if (user1 == user && password == password1)
  128. {
  129. cout << "验证通过" << endl;
  130. }
  131. else
  132. {
  133. cout << "验证失败" << endl;
  134. return false;
  135. }
  136. return true;
  137. }
  138. void entering_f()//录入航班
  139. {
  140. if (authentication())
  141. {
  142. date_init();
  143. sql_init();
  144. sql = "insert into test_03.flight_information values" + sql;
  145. const char* char_sql = sql.data();
  146. if (mysql_query(&mysql, char_sql)) // 执行SQL语句
  147. {
  148. cout << "插入失败:" << mysql_error(&mysql) << endl;
  149. }
  150. else
  151. {
  152. cout << "插入成功..." << endl;
  153. }
  154. cout << "录入新航班之后的航班信息" << endl;
  155. inquire_mysql();
  156. }
  157. }
  158. void alter_f()//修改航班
  159. {
  160. if (authentication())
  161. {
  162. cout << "输入主键id" << endl;
  163. cin >> id;
  164. cout << "请输入起飞日期" << endl;
  165. cin >> f_date;
  166. cout << "请输入起飞时刻" << endl;
  167. cin >> f_off_time;
  168. cout << "请输入到达时刻" << endl;
  169. cin >> f_reach_time;
  170. cout << "请输入票价" << endl;
  171. cin >> f_price;
  172. cout << "请输入折扣票数" << endl;
  173. cin >> f_discount_num;
  174. cout << "请输入剩余座位" << endl;
  175. cin >> f_remaining_seat;
  176. sql += "f_date = ";
  177. sql += f_date;
  178. sql += ", f_off_time = ";
  179. sql += f_off_time;
  180. sql += ", f_reach_time = ";
  181. sql += f_reach_time;
  182. sql += ", f_price = ";
  183. sql += to_string(f_price);
  184. sql += ", f_discount_num = ";
  185. sql += to_string(f_discount_num);
  186. sql += ", f_remaining_seat = ";
  187. sql += to_string(f_remaining_seat);
  188. sql = "update flight_information set " + sql + " where id = " + to_string(id) + ";";
  189. const char* char_sql = sql.data();
  190. if (mysql_query(&mysql, char_sql)) // 执行SQL语句
  191. {
  192. cout << "修改失败:" << mysql_error(&mysql) << endl;
  193. }
  194. else
  195. {
  196. cout << "修改成功..." << endl;
  197. }
  198. cout << "修改之后的航班信息" << endl;
  199. inquire_mysql();
  200. }
  201. }
  202. void delete_f()//删除航班
  203. {
  204. if (authentication())
  205. {
  206. cout << "输入需要删除的主键id" << endl;
  207. cin >> id;
  208. sql += "delete from flight_information where id = " + to_string(id) + ";";
  209. const char* char_sql = sql.data();
  210. if (mysql_query(&mysql, char_sql)) // 执行SQL语句
  211. {
  212. cout << "删除失败:" << mysql_error(&mysql) << endl;
  213. }
  214. else
  215. {
  216. cout << "删除成功..." << endl;
  217. }
  218. cout << "删除之后的航班信息" << endl;
  219. inquire_mysql();
  220. }
  221. }
  222. void delete_mysql()//释放资源
  223. {
  224. //释放结果集
  225. mysql_free_result(res);
  226. //关闭数据库
  227. mysql_close(&mysql);
  228. }
  229. void menu()
  230. {
  231. cout << "无需登陆" << endl;
  232. cout << "1.查询航班信息" << endl;
  233. cout << "需要登陆" << endl;
  234. cout << "2.录入航班信息" << endl;
  235. cout << "3.修改航班信息" << endl;
  236. cout << "4.删除航班信息" << endl;
  237. cout << "0.退出" << endl;
  238. }
  239. };
  240. int main()
  241. {
  242. mysql_f ms;
  243. ms.join_mysql();
  244. int select = 0;
  245. cout << "请输入你要进行的操作" << endl;
  246. while (1)
  247. {
  248. ms.menu();
  249. cin >> select;
  250. switch (select)
  251. {
  252. case 1:
  253. ms.inquire_mysql();
  254. system("pause");
  255. system("cls");
  256. cout << "请输入你要进行的操作" << endl;
  257. break;
  258. case 2:
  259. ms.entering_f();
  260. system("pause");
  261. system("cls");
  262. cout << "请输入你要进行的操作" << endl;
  263. break;
  264. case 3:
  265. ms.alter_f();
  266. system("pause");
  267. system("cls");
  268. cout << "请输入你要进行的操作" << endl;
  269. break;
  270. case 4:
  271. ms.delete_f();
  272. system("pause");
  273. system("cls");
  274. cout << "请输入你要进行的操作" << endl;
  275. break;
  276. case 0:
  277. exit(0);
  278. break;
  279. default:
  280. break;
  281. }
  282. }
  283. ms.delete_mysql();
  284. system("pause");
  285. return 0;
  286. }

 

 

 

 

 本次实验仍有需要不足的地方,还望读者,有自己的想法。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/173226
推荐阅读
相关标签
  

闽ICP备14008679号