C語言實現圖書館管理系統
本文實例為大傢分享瞭C語言實現圖書館管理系統的具體代碼,供大傢參考,具體內容如下
全部代碼如下:
#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(); }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。