通讯录
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
void menu() //菜单
{
printf("******************* welcome *********************\n");
printf("******************* 菜单 *********************\n");
printf("******* 1--添加联系人 ***********\n");
printf("******* 2--查找联系人 ***********\n");
printf("******* 3--显示全部联系人 ***********\n");
printf("******* 4--删除联系人 ***********\n");
printf("******* 5--清空联系人 ***********\n");
printf("******* 0--退出通讯录 ***********\n");
printf("****************************************************\n");
}
void create_table(sqlite3 * db)
{
char *sql;
char *errmsg;
int rec;
sql = "create table if not exists mytable(name text,sex text,age integer,address text,tel text primary key,qq text);";
rec = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(rec != SQLITE_OK)
{
printf("create error! %s\n",errmsg);
exit(-1);
}
}
void insert_book(sqlite3 *db) //添加联系人
{
char name[20];
char sex[5];
char address[50];
char tel[15];
char qq[15];
int age;
char sql[200];
char *errmsg;
int rec;
char c = 0;
do
{
printf("请输入姓名,性别,年龄,地址,电话,qq:\n");
scanf("%s%s%d%s%s%s",name,sex,&age,address,tel,qq);
sprintf(sql,"insert into mytable(name,sex,age,address,tel,qq) values('%s','%s',%d,'%s','%s','%s');",name,sex,age,address,tel,qq);
rec = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(rec != SQLITE_OK)
{
printf("添加失败!%s\n",errmsg);
exit(-1);
}
printf("是否继续添加?(y/n):");
while((c = getchar()) != EOF && c != '\n');
c = getchar();
}while(c == 'y');
}
void search_man(sqlite3 * db) //按姓名查找联系人
{
char sql[100];
char **result;
int n_row;
int n_col;
char *errmsg;
char name[20];
int i;
int rec;
char c = 0;
do
{
printf("请输入姓名:\n");
scanf("%s",name);
sprintf(sql,"select * from mytable where name = '%s';",name);
rec = sqlite3_get_table(db,sql,&result,&n_row,&n_col,&errmsg);
if(rec != SQLITE_OK)
{
printf("查询错误!%s\n",errmsg);
exit(-1);
}
printf("n_row = %d,n_col = %d\n",n_row,n_col);
printf("result is:\n");
for(i = 0;i < n_col;i++)
{
printf("%15s",result[i]);
}
printf("\n");
for(;i < (n_row + 1) * n_col;i++)
{
printf("%15s",result[i]);
if((i + 1) % n_col == 0)
{
printf("\n");
}
}
printf("是否继续查找(y/n):\n");
while((c = getchar()) != EOF && c != '\n');
c = getchar();
}while(c == 'y');
sqlite3_free_table(result);
}
void search_all(sqlite3 * db) //显示所有联系人
{
char *sql;
char **result;
int n_row;
int n_col;
char *errmsg;
int i;
int rec;
sql = "select * from mytable;";
rec = sqlite3_get_table(db,sql,&result,&n_row,&n_col,&errmsg);
if(rec != SQLITE_OK)
{
printf("get table error!,%s\n",errmsg);
exit(-1);
}
printf("n_row = %d,n_col = %d\n",n_row,n_col);
printf("result is:\n");
for(i = 0;i < n_col;i++)
{
printf("%15s",result[i]);
}
printf("\n");
for(;i < (n_row + 1) * n_col;i++)
{
printf("%15s",result[i]);
if((i + 1) % n_col == 0)
{
printf("\n");
}
}
sqlite3_free_table(result);
}
void delete_man(sqlite3 *db) //删除联系人(按姓名)
{
char sql[100];
char *errmsg;
char c = 0;
char name[20];
int rec;
do
{
printf("请输入要删除的联系人姓名:\n");
scanf("%s",name);
sprintf(sql,"delete from mytable where name = '%s';",name);
rec = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(rec != SQLITE_OK)
{
printf("删除失败!%s\n",errmsg);
exit(-1);
}
printf("是否继续删除(y/n):\n");
while((c = getchar()) != EOF && c != '\n');
c = getchar();
}while(c == 'y');
}
void delete_book(sqlite3 *db) //清空联系人
{
char *sql;
char *errmsg;
int rec;
sql = "delete from mytable;";
rec = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(rec != SQLITE_OK)
{
printf("清空失败!%s\n",errmsg);
exit(-1);
}
}
int main()
{
menu();
sqlite3 * db;
int rec;
rec = sqlite3_open("myaddressbook.db",&db);
if(rec != SQLITE_OK)
{
printf("open error! %s\n",sqlite3_errmsg(db));
exit(-1);
}
create_table(db);
int choice;
while(1)
{
printf("请输入您想进行的操作:\n");
scanf("%d",&choice);
switch(choice)
{
case 0:
printf("欢迎下次使用!\n");
return 0;
break;
case 1:
insert_book(db); //添加联系人
break;
case 2:
search_man(db); //按姓名查找联系人
break;
case 3:
search_all(db); //显示所有联系人
break;
case 4:
delete_man(db); //删除联系人(按姓名)
break;
case 5:
delete_book(db); //清空联系人
break;
default:
printf("输入有误!重新输入!\n");
break;
}
}
sqlite3_close(db);
return 0;
}
演示结果:
[root@bogon data_address_book]# ./book
******************* welcome *********************
******************* 菜单 *********************
******* 1--添加联系人 ***********
******* 2--查找联系人 ***********
******* 3--显示全部联系人 ***********
******* 4--删除联系人 ***********
******* 5--清空联系人 ***********
******* 0--退出通讯录 ***********
****************************************************
请输入您想进行的操作:
3
n_row = 1,n_col = 6
result is:
name sex age address tel qq
gxc girl 21 cz 1865648644 15512155
请输入您想进行的操作:
1
请输入姓名,性别,年龄,地址,电话,qq:
wfx boy 21 nt 15251341714 1151864949
是否继续添加?(y/n):y
请输入姓名,性别,年龄,地址,电话,qq:
gxh boy 21 ha 153264664 185546331
是否继续添加?(y/n):n
请输入您想进行的操作:
3
n_row = 3,n_col = 6
result is:
name sex age address tel qq
gxc girl 21 cz 1865648644 15512155
wfx boy 21 nt 15251341714 1151864949
gxh boy 21 ha 153264664 185546331
请输入您想进行的操作:
0
欢迎下次使用!