本文实例为大家分享了C++实现单词管理系统的具体代码,供大家参考,具体内容如下
实现功能
- 退出
- 添加单词
- 删除单词
- 修改单词
- 查询单词
- 排序单词
- 显示单词
简述
单词管理系统使用了C++语言连接MySQL数据库实现常规CRUD操作,界面为控制台窗体+文字显示的方式。
测试环境
使用Win10 + Code::Blocks IDE编写。
运行截图
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
#include <stdio.h> #include <winsock2.h> //进行网络连接 #include <mysql.h> //MySQL C API访问mysql数据库 #pragma comment (lib, "libmysql.lib") #define N 50 typedef struct Dictionary { char id[50]; char eng[100]; char chi[100]; } Dictionary; //变量配置 MYSQL *conn; //数据库连接句柄 MYSQL_RES *res; //执行数据库语言结果 MYSQL_ROW row; //存放一个数据记录 char * server = "localhost" ; //本地连接 char * user = "root" ; // char * password = "yan19991001" ; //mysql密码 char * database = "dictionary" ; //数据库名 int t,rc; char query[N]; //需要查询的语句 void readEng(); void addEng(); void delEng(); void modEng(); void seaEng(); void sort(); void sort(){ char id[N],eng[N],chi[N]; sprintf (query, "select * from test order by eng" ); printf ( "查询语句:%s\n" ,query); rc = mysql_query(conn,query); if (0 != rc) { printf ( "mysql_real_query(): %s\n" , mysql_error(conn)); return -1; } else { printf ( "查询结果:\n" ); res = mysql_use_result(conn); //获取结果 if (res) { while ((row = mysql_fetch_row(res)) != NULL) { //printf("num=%d\n",mysql_num_fields(res));//列数 for (t = 0; t < mysql_num_fields(res); t++) printf ( "%8s " , row[t]); printf ( "\n" ); } } mysql_free_result(res); } } void addEng() { char id[N],eng[N],chi[N]; printf ( "请输入要增加的词典信息:\n" ); printf ( "编号:\n" ); scanf ( "%s" ,id); printf ( "单词:\n" ); scanf ( "%s" ,eng); printf ( "中文释义:\n" ); scanf ( "%s" ,chi); sprintf (query, "insert into test values('%s','%s','%s')" ,id,eng,chi); printf ( "%s" ,query); rc = mysql_query(conn,query); if (0 != rc) { printf ( "mysql_real_query(): %s\n" , mysql_error(conn)); return -1; } else { printf ( "添加成功!\n" ); } //mysql_close(conn); //断开数据库 } void delEng(){ char id[N]; printf ( "请输入你要删除的单词编号:" ); scanf ( "%s" ,id); sprintf (query, "delete from test where id = '%s'" ,id); printf ( "查询语句:%s\n" ,query); rc = mysql_query(conn,query); if (0 != rc) { printf ( "mysql_real_query(): %s\n" , mysql_error(conn)); return -1; } else { printf ( "删除成功!\n" ); } } void modEng(){ char id[N],eng[N],chi[N]; printf ( "请输入你要修改的单词编号:" ); scanf ( "%s" ,id); printf ( "单词:\n" ); scanf ( "%s" ,eng); printf ( "中文释义:\n" ); scanf ( "%s" ,chi); sprintf (query, "update test set eng = '%s',chi = '%s' where id = '%s'" ,eng,chi,id); printf ( "查询语句:%s\n" ,query); rc = mysql_query(conn,query); if (0 != rc) { printf ( "mysql_real_query(): %s\n" , mysql_error(conn)); return -1; } else { printf ( "修改成功!\n" ); } } void seaEng(){ char id[N],eng[N],chi[N]; printf ( "请输入你要查询的单词编号:" ); scanf ( "%s" ,id); sprintf (query, "select * from test where id = '%s'" ,id); printf ( "查询语句:%s\n" ,query); rc = mysql_query(conn,query); if (0 != rc) { printf ( "mysql_real_query(): %s\n" , mysql_error(conn)); return -1; } else { printf ( "查询结果:\n" ); res = mysql_use_result(conn); //获取结果 if (res) { while ((row = mysql_fetch_row(res)) != NULL) { //printf("num=%d\n",mysql_num_fields(res));//列数 for (t = 0; t < mysql_num_fields(res); t++) printf ( "%8s " , row[t]); printf ( "\n" ); } } mysql_free_result(res); } } void init() { conn = mysql_init(NULL); //句柄初始化 if (!mysql_real_connect(conn, server, user, password, database, 3306, NULL, 0)) //判断是否连接成功 { printf ( "Error connecting to database:%s\n" , mysql_error(conn)); } else { printf ( "Connected...\n" ); } //字符编码,解决乱码 if (!mysql_set_character_set(conn, "gbk" )) { printf ( "New client character set: %s\n" , mysql_character_set_name(conn)); } } void readEng() { char * query = "select * from test" ; //需要查询的语句 if (mysql_query(conn, query)) { printf ( "错误信息:%s\n" , mysql_error(conn)); } else { printf ( "查询结果:\n" ); res = mysql_use_result(conn); //获取结果 if (res) { while ((row = mysql_fetch_row(res)) != NULL) { //printf("num=%d\n",mysql_num_fields(res));//列数 for (t = 0; t < mysql_num_fields(res); t++) printf ( "%8s " , row[t]); printf ( "\n" ); } } mysql_free_result(res); } } void menu() { int choice; char id[20]; do { printf ( "------------------------------\n" ); printf ( "0、退出\n" ); printf ( "1、添加单词\n" ); printf ( "2、删除单词\n" ); printf ( "3、修改单词\n" ); printf ( "4、查询单词\n" ); printf ( "5、排序单词\n" ); printf ( "6、显示单词\n" ); printf ( "------------------------------\n" ); printf ( "请输入选择:" ); scanf ( "%d" ,&choice); //根据choice的值选取功能 switch (choice) { case 0: exit (0); break ; case 1: addEng(); break ; case 2: delEng(); break ; case 3: modEng(); break ; case 4: seaEng(); break ; case 5: sort(); break ; case 6: readEng(); break ; default : printf ( "输入错误!" ); } system ( "pause" ); system ( "cls" ); } while (choice != 0); } int main() { init(); menu(); return 0; } |
数据库代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Type : MySQL Source Server Version : 50725 Source Host : localhost:3306 Source Schema : dictionary Target Server Type : MySQL Target Server Version : 50725 File Encoding : 65001 Date : 28/06/2021 13:44:35 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for test -- ---------------------------- DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` varchar (50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL , `eng` varchar (255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL , `chi` varchar (255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL , PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic ; -- ---------------------------- -- Records of test -- ---------------------------- INSERT INTO `test` VALUES ( '1' , 'adopt' , '领养' ); INSERT INTO `test` VALUES ( '2' , 'pen' , '钢笔' ); INSERT INTO `test` VALUES ( '3' , 'apple' , '苹果' ); INSERT INTO `test` VALUES ( '4' , 'borrow' , '借阅' ); INSERT INTO `test` VALUES ( '5' , 'electric' , '电力' ); SET FOREIGN_KEY_CHECKS = 1; |
总结
代码还是比较简单的,主要是不同编译器,它所对应的驱动方式会有所不同。因此如果想要移植到其它的IDE如: VC6++、VS、DEV 等,可能需要一些处理操作,还要添加数据库连接驱动和库函数。当然,难点也就在于获取ODBC连接,这块还是需要一些时间琢磨的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_40491534/article/details/120113880