Linux C和MySQL数据库写的一个“电话本管理程序”,模拟手机平台写一个”电话本管理程序“1.显示操作菜单2.根据菜单可以做出如下操作a.显示出所有联系人电话号码b.根据姓名查找电话号码c.支持按照姓氏查找电话号码d.允许修改已经查找到联系人的号码e.允许删除一个联系人f.允许增加一个联系人的号码,如果联系人已经存在,一个联系人最多保存三个号码3.退出程序
- #include<stdio.h>
- #include<stdlib.h>
- #include<mysql.h>
- #include<string.h>
-
- void createTable(MYSQL * conn)//建立数据库和表
- {
-
- mysql_query(conn,"drop database if exists phonebook;");
- mysql_query(conn,"create database phonebook;");
- mysql_query(conn,"use phonebook;");
- mysql_query(conn,"create table contacts(Name varchar(20) default "no name",Telnum varchar(20));");
- mysql_query(conn,"insert into contacts values("gw","15974165855");");
- mysql_query(conn,"insert into contacts values("zp","13420995347");");
- }
-
- void show_menuAll()
- {
- printf("==========欢迎来到手机数据库===========
");
- printf(" a.显示所有联系人
b.根据姓名查找号码
c.添加联系人及号码
d.删除联系人
e.修改联系人号码
f.退出电话簿
");
- printf("请选择 ");
- }
-
- void show_menuName()
- {
- printf(" 1.安姓名精确查找
2.按姓氏模糊查找
3.返回
");
- printf("请选择查找方式:
");
- }
-
- void display_select(MYSQL * conn)//屏幕显示select查询出来的信息
- {
- MYSQL_RES *res;//声明存储结构
- res=mysql_store_result(conn);
- int row1=mysql_num_rows(res);
- int col=mysql_num_fields(res);
- // printf("row=%d col=%d
",row1,col);
- MYSQL_ROW row;//成名取数据的结构,不是指针
- int i;
- while((row=mysql_fetch_row(res))!=NULL)
- {//判断是否数据全部去完,函数返回NULL表示数据取完
- for(i=0;i<col;i++)
- {
- // printf("%s %s
",row[0],row[1]);
- printf("%s ",row[i]);
- }
- printf("
");
- }
- mysql_free_result(res);
- }
-
- void select_By_name(MYSQL *conn)//按姓名查询联系人
- {
- char name[20];
- scanf("%s",name);
- char sql1[64]="select Telnum from contacts where Name="";
- char sql2[4]="";";
- strcat(sql1,name);
- strcat(sql1,sql2);
- mysql_query(conn,sql1);
- display_select(conn);
- }
- void select_By_firstname(MYSQL *conn)//按姓氏查询联系人
- {
- char name[20];
- scanf("%s",name);
- char sql1[64]="select Telnum from contacts where Name like "";
- char sql2[4]="%";";
- strcat(sql1,name);
- strcat(sql1,sql2);
- mysql_query(conn,sql1);
- display_select(conn);
- }
- void insert_Contacts(MYSQL *conn)//插入联系人信息
- {
- char tel[20];
- char name[20];
- scanf("%s%s",&name,&tel);
- //insert into contacts values("gw","123213");
- char sql1[64]="insert into contacts values("";
- char sql2[4]="","";
- char sql3[4]="");";
- strcat(sql1,name);
- strcat(sql1,sql2);
- strcat(sql1,tel);
- strcat(sql1,sql3);
- mysql_query(conn,sql1);
- }
- void delete_by_name(MYSQL *conn)//按姓名删除联系人
- {
- char name[20];
- scanf("%s",name);
- char sql1[64]="delete from Telnum where Name="";
- char sql2[4]="";";
- strcat(sql1,name);
- strcat(sql1,sql2);
- mysql_query(conn,sql1);
- printf("删除成功!
");
- }
-
- void alter_by_name(MYSQL *conn)//修改联系人号码
- {
- char name[20];
- char tel[20];
- scanf("%s%s",name,tel);
- char sql1[64]="update student3 set Telnum="";
- char sql2[64]=""where Name="";
- char sql3[4]="";";
- strcat(sql1,tel);
- strcat(sql1,sql2);
- strcat(sql1,sql3);
- mysql_query(conn,sql1);
- printf("修改成功!
");
- }
- //主函数
- int main(int argc,char *argv[])
- {
- MYSQL * conn=NULL;
- conn=mysql_init(NULL);
- if(mysql_real_connect(conn,"localhost","root","root","test",3306,NULL,0)==NULL)
- {
- printf("connect error %s
",mysql_error(conn));
- return -1;
- }
- createTable(conn);
-
- while(1)
- {
-
- show_menuAll();
- char ch1;//接收屏幕选择;
- scanf("%c",&ch1);
-
- if(ch1 == "a")//显示所有联系人信息
- {
- mysql_query(conn,"select * from contacts;");
- display_select(conn);
- printf(" 1.返回
2.退出
");
- getchar();
- char i;
- scanf("%c",&i);
- if(i=="1")
- continue;
- else
- break;
- }
-
- if(ch1 == "b")//根据姓名查找号码
- {
- show_menuName();
- getchar();
- char ch2;//
- scanf("%c",&ch2);
- if(ch2 == "1")//按姓名全称查找
- {
- while(1)
- {
- printf(" 青输入联系人全称:
");
- select_By_name(conn);
- printf(" 1.继续查找
2.退出
");
- getchar();
- char l;
- scanf("%c",&l);
- if(l=="1")
- continue;
- else
- break;
-
-
- }
-
- }
- else if(ch2 == "2")//按姓氏模糊查找
- {
- while(1)
- {
- printf(" 清输入联系人姓氏:
");
- select_By_firstname(conn);
- printf(" 1.继续查找
2.退出
");
- getchar();
- char m;
- scanf("%c",&m);
- if(m=="1")
- continue;
- else
- break;
-
- }
-
- }
- else if(ch2 == "3")
- {
- continue;
- }
- }
- if(ch1 == "c")//添加联系人及号码
- {
-
- while(1)
- {
- insert_Contacts(conn);
- printf("是否继续添加y/n
");
- getchar();
- char j;
- scanf("%c",&j);
- if(j == "y")
- continue;
- else
- break;
- }
- continue;
- }
- if(ch1 == "d")//删除联系人
- {
- while(1)
- {
- printf(" 请输入需要删除的联系人姓名:
");
- delete_by_name(conn);
- printf("是否继续删除y/n
");
- getchar();
- char n;
- scanf("%c",&n);
- if(n == "y")
- continue;
- else
- break;
- }
- continue;
-
- }
- if(ch1 == "e")//修改联系人号码
- {
- while(1)
- {
- printf(" 请输入需要修改的联系人姓名:
");
- alter_by_name(conn);
- printf("是否继续删除y/n
");
- getchar();
- char k;
- scanf("%c",&k);
- if(k == "y")
- continue;
- else
- break;
- }
- continue;
-
-
- }
- if(ch1 == "f")
- {
- printf("退出成功
");
- break;
- }
- }
- mysql_close(conn);
- return 0;
- }