本文实例为大家分享了C语言实现图书馆管理系统的具体代码,供大家参考,具体内容如下
全部代码如下:
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include <mysql.h>
#pragma comment (lib,"libmysql.lib")
//#pragma comment ( lib,"xxx.lib" )
//表示鏈接xxx.lib這個庫,告訴編譯器你要用到xxx.lib庫
//和在工程設置裡寫上鍊入xxx.lib的效果一樣,不過這種方法寫的程序別人在使用你的代碼的時候就不用再設置工程settings了。
MYSQL mysql;
MYSQL_RES *result; //定义结果集变量
MYSQL_ROW row; //定义行变量
char ch[2];
void inquire() {
void menu();
printf ( "\n\t显示主清单<y/n>\n" );
scanf ( "%s" , ch);
if ( strcmp (ch, "y" ) == 0 || strcmp (ch, "Y" ) == 0) {
menu();
}
else exit (0);
}
void ShowAll() {
system ( "cls" );
mysql_init(&mysql);
mysql_query(&mysql, "set names 'utf8'" );
if (!mysql_real_connect(&mysql, "localhost" , "root" , "密码" , "db_book" , 0, NULL, 0))
printf ( "\ncan't connect db_book!\n" );
else
{
if (mysql_query(&mysql, "select * from tb_book" ))
{
printf ( "查询失败\n" );
}
else {
puts ( "\t|=====================================================|\n" );
puts ( "\t| 显 示 所 有 图 书 信 息 |\n" );
puts ( "\t|=====================================================|\n" );
puts ( "\t| 图书编号 图书名 作者 出版社 |\n" );
result = mysql_store_result(&mysql);
if (mysql_num_rows(result)) {
while (row = mysql_fetch_row(result))
{
printf ( "\t\t%s\t %s\t %s\t %s\n" , row[0], row[1], row[2], row[3]);
}
}
mysql_free_result(result);
}
puts ( "\t|=====================================================|\n" );
getchar ();
mysql_close(&mysql); //关闭连接
}
}
void AddBook() {
char id[10];
char bookname[10],author[10], bookconcern[10], *sql;
char dest[100] = { " " };
system ( "cls" );
mysql_init(&mysql);
if (!mysql_real_connect(&mysql, "localhost" , "root" , "xin088748*" , "db_book" , 0, NULL, 0))
printf ( "can not connect\n" );
else {
if (mysql_query(&mysql, "select * from tb_book" )) {
printf ( "can not open\n" );
}
else
{
result = mysql_store_result(&mysql);
mysql_num_rows(result);
row = mysql_fetch_row(result);
printf ( "\tID:" );
scanf ( "%s" , id);
if (mysql_num_rows(result)) {
do {
if (! strcmp (id, row[0])) {
printf ( "\t记录存在" );
getchar ();
mysql_free_result(result);
mysql_close(&mysql);
return ;
}
} while (row = mysql_fetch_row(result));
}
printf ( "\t图书名:" );
scanf ( "%s" , bookname);
printf ( "\t作者:" );
scanf ( "%s" , author);
printf ( "\t出版社:" );
scanf ( "%s" , bookconcern);
sql = "insert into tb_book (ID,bookname,author,bookconcern) values('" ;
strcat (dest, sql);
strcat (dest, id);
strcat (dest, "','" );
strcat (dest, bookname);
strcat (dest, "','" );
strcat (dest, author);
strcat (dest, "','" );
strcat (dest, bookconcern);
strcat (dest, "')" );
if (mysql_query(&mysql, dest))
//fprintf(stderr, "error!", mysql_error(&mysql));
printf ( "error!\n" );
else
{
mysql_free_result(result);
}
}
mysql_close(&mysql); //关闭连接
}
}
void ModifyBook() {
system ( "cls" );
char dest[100];
mysql_init(&mysql);
mysql_query(&mysql, "set name 'utf8'" );
if (!mysql_real_connect(&mysql, "localhost" , "root" , "xin088748*" , "db_book" , 0, NULL, 0)) { puts ( "can't net mysql\n" );
return ;
}
else {
char id[10],*sql;
char dest[100] = { " " };
printf ( "请输入你要修改的图书编号:" );
scanf ( "%s" , id);
sql = "select * from tb_book where ID = " ;
strcat (dest, sql);
strcat (dest, id);
if (mysql_query(&mysql, dest)) {
printf ( "\n 查询数据表失败\n" );
}
else {
result = mysql_store_result(&mysql);
if (mysql_num_rows(result)) {
puts ( "\t|=====================================================|\n" );
puts ( "\t| 显 示 所 有 图 书 信 息 |\n" );
puts ( "\t|=====================================================|\n" );
puts ( "\t| 图书编号 图书名 作者 出版社 |\n" );
while (row = mysql_fetch_row(result)) {
printf ( "\t\t%s\t %s\t %s\t %s\n" , row[0], row[1], row[2], row[3]);
}
printf ( "Mondify? <y/n>\n" );
scanf ( "%s" , ch);
if (! strcmp (ch, "Y" ) || ! strcmp (ch, "y" )) {
char bookname[10], author[10], bookconcern[10];
char dest1[100] = { " " };
printf ( "\t图书名:" );
scanf ( "%s" , bookname);
printf ( "\t作者:" );
scanf ( "%s" , author);
printf ( "\t出版社:" );
scanf ( "%s" , bookconcern);
sql = "update tb_book set bookname = '" ;
strcat (dest1, sql);
strcat (dest1, bookname);
strcat (dest1, "',author = '" );
strcat (dest1, author);
strcat (dest1, "',bookconcern = '" );
strcat (dest1, bookconcern);
strcat (dest1, "' where ID = '" );
strcat (dest1, id);
strcat (dest1, "'" );
if (mysql_query(&mysql,dest1)) printf ( "修改失败\n" );
else printf ( "修改成功\n" );
}
}
else
{
printf ( "没有发现要修改的信息\n" );
}
mysql_free_result(result);
}
}
mysql_close(&mysql);
}
void DeleteBook() {
system ( "cls" );
mysql_init(&mysql);
if (!mysql_real_connect(&mysql, "localhost" , "root" , "xin088748*" , "db_book" , 0, NULL, 0)) {
printf ( "can't net mysql\n" );
}
else {
printf ( "你想要删除的编号:" );
char id[10];
scanf ( "%s" , id);
char dest[100] = { " " };
char *sql;
sql = "select * from tb_book where ID = " ;
strcat (dest, sql);
strcat (dest, id);
if (mysql_query(&mysql, dest)) {
printf ( "\n 查询数据表失败\n" );
}
else {
result = mysql_store_result(&mysql);
if (mysql_num_rows(result)) {
puts ( "\t|=====================================================|\n" );
puts ( "\t| 显 示 所 有 图 书 信 息 |\n" );
puts ( "\t|=====================================================|\n" );
puts ( "\t| 图书编号 图书名 作者 出版社 |\n" );
while (row = mysql_fetch_row(result)) {
printf ( "\t\t%s\t %s\t %s\t %s\n" , row[0], row[1], row[2], row[3]);
}
printf ( "Delete? <y/n>\n" );
scanf ( "%s" , ch);
if (! strcmp (ch, "Y" ) || ! strcmp (ch, "y" )) {
char dest1[100] = { " " };
sql = "delete from tb_book where ID = " ;
strcat (dest1, sql);
strcat (dest1, id);
printf ( "\n%s\t" , dest1);
if (mysql_query(&mysql, dest1)) printf ( "删除失败\n" );
else printf ( "删除成功\n" );
}
}
else puts ( "没有查询到此数据\n" );
mysql_free_result(result);
}
}
mysql_close(&mysql);
}
void Querybook() {
system ( "cls" );
mysql_init(&mysql);
if (!mysql_real_connect(&mysql, "localhost" , "root" , "xin088748*" , "db_book" , 0, NULL, 0)) {
puts ( "can't net mysql\n" );
return ;
}
else {
char id[10];
char *sql, dest[100] = { " " };
printf ( "请输入要查询的图书编号:\n" );
scanf ( "%s" , id);
sql = "select * from tb_book where ID = " ;
strcat (dest, sql);
strcat (dest, id);
if (mysql_query(&mysql, dest)) printf ( "查询失败\n" );
else {
result = mysql_store_result(&mysql);
if (mysql_num_rows(result)) {
puts ( "\t|=====================================================|\n" );
puts ( "\t| 显 示 所 有 图 书 信 息 |\n" );
puts ( "\t|=====================================================|\n" );
puts ( "\t| 图书编号 图书名 作者 出版社 |\n" );
while (row = mysql_fetch_row(result)) {
printf ( "\t\t%s\t %s\t %s\t %s\n" , row[0], row[1], row[2], row[3]);
}
}
else puts ( "没有发现要查询的数据\n" );
mysql_free_result(result);
}
}
mysql_close(&mysql);
}
void menu() {
puts ( "\t|=====================================================|\n" );
puts ( "\t| Welcome To Lemon Books System |\n" );
puts ( "\t|=====================================================|\n" );
puts ( "\t| 1 - 显示所有图书信息 |\n" );
puts ( "\t| 2 - 添加图书信息 |\n" );
puts ( "\t| 3 - 修改图书信息 |\n" );
puts ( "\t| 4 - 删除图书信息 |\n" );
puts ( "\t| 5 - 查询图书信息 |\n" );
puts ( "\t| 6 - 退出图书系统 |\n" );
puts ( "\t|=====================================================|\n\n" );
puts ( "\tENTER YOUR CHOICE<1-6>\n" );
int i;
scanf ( "%d" , &i);
while (i > 0 && i < 7) {
switch (i) {
case 1:ShowAll(); inquire(); break ;
case 2:AddBook(); inquire(); break ;
case 3:ModifyBook(); inquire(); break ;
case 4:DeleteBook(); inquire(); break ;
case 5:Querybook(); inquire(); break ;
default : puts ( "enter error!please enter again\n" ); exit (0);
}
scanf ( "%d" ,&i);
}
}
int main()
{
menu();
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_43474701/article/details/119106424