通讯录:
#include
#include
#include
sqlite3 *ppdb;
char * errmsg;
char sql[32];
int ret;
int callback(void * a,int argc,char**argv,char **col)//注意void * 要加个参数;
{
int i;
for(i=0;i { printf("%s=%s\n",col[i],argv[i]); } return 0; } void xianshi()//显示都要调用回调函数; { bzero(sql,32); strcpy(sql," select *from tongxunlu;"); sqlite3_exec(ppdb,sql,callback,NULL,&errmsg); } void chaxun() { printf("1-通过名字查询\n"); printf("2-通过号码查询\n"); printf("请输入数字选择查询方式\n"); int g; scanf("%d",&g); char buf[125]; bzero(buf,125); if(g==1) { printf("请输入名字\n"); scanf("%s",buf); //fgets(buf,125,stdin);//用这个buf后面有回车,不能成功调用函数; sprintf(sql,"select * from tongxunlu where name like '%%%s%%';",buf);//c语言识别的时候两个%号,相当于一个百分号,这里用模糊搜索法; ret=sqlite3_exec(ppdb,sql,callback,NULL,&errmsg); if(ret!=SQLITE_OK ) { printf("error2 =%s\n",errmsg); sqlite3_free(errmsg); } } if(g==2) { printf("请输入号码\n"); scanf("%s",buf); //fgets(buf,125,stdin); sprintf(sql,"select * from tongxunlu where num='%s';",buf); sqlite3_exec(ppdb,sql,callback,NULL,&errmsg); ret=sqlite3_exec(ppdb,sql,callback,NULL,&errmsg); if(ret!=SQLITE_OK ) { printf("error2 =%s\n",errmsg); sqlite3_free(errmsg); } } } char buf1[125]; char buf2[125]; void tianjia() { printf("请分别输入联系人的名字和号码\n"); scanf("%s",buf1); scanf("%s",buf2); bzero(sql,32); sprintf(sql,"insert into tongxunlu values('%s','%s');",buf1,buf2); sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg); } void xiugai() { bzero(sql,32); bzero(buf1,125); bzero(buf2,125); printf("1-修改联系人名字\n"); printf("2-修改联系人号码\n"); printf("请进行选择\n"); int h; scanf("%d",&h); if(h==1) { printf("请输入要变化成的名字和不用修改的号码\n"); scanf("%s",buf1); scanf("%s",buf2); sprintf(sql,"update tongxunlu set name='%s' where num='%s';",buf1,buf2); sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg); } if(h==2) { bzero(buf1,125); bzero(buf2,125); printf("请输入不用修改的名字和要变化成的号码和\n"); scanf("%s",buf1); scanf("%s",buf2); sprintf(sql,"update tongxunlu set num='%s' where name='%s';",buf2,buf1); sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg); } } void shanchu() { printf("请输入你要删除联系人名字\n"); bzero(buf1,125); scanf("%s",buf1); sprintf(sql,"delete from tongxunlu where name='%s';",buf1); sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg); } void chuanjian() { strcpy(sql,"CREATE TABLE tongxunlu(name varchar(10) NOT NULL,num varchar(13) NOT NULL);"); ret=sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg); if(ret!=SQLITE_OK ) { printf("error2 =%s\n",errmsg); sqlite3_free(errmsg); } } void chushihua() { bzero(sql,32);//插入三条联系人信息 strcpy(sql,"insert into tongxunlu values('曹博','18163980137');"); sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg); bzero(sql,32); strcpy(sql,"insert into tongxunlu values('何煌','18163980138');"); sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg); bzero(sql,32); strcpy(sql,"insert into tongxunlu values('曾翔武','17352641850');"); sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg); } void shanchubiao() { strcpy(sql,"drop table tongxunlu;"); sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg); } int main() { char *filename="tongxunlu.db";//建表 ret = sqlite3_open("tongxunlu.db", &ppdb );//share文件下面会有这个数据库文件,可以用sqlite3打开进行相关的操作; if(ret!=SQLITE_OK ) { printf("error1 =%s\n",errmsg); sqlite3_free(errmsg); } while(1) { printf("******曹博的通讯录*******\n"); printf("0-退出通讯录系统\n"); printf("1-显示所有人\n"); printf("2-查询某个联系人\n"); printf("3-添加联系人\n"); printf("4-修改联系人\n"); printf("5-删除联系人\n"); printf("6-创建表\n"); printf("7-初始化插入三条数据\n"); printf("8-删除表\n"); printf("请输入你要进行的操作\n"); int i; scanf("%d",&i); switch(i) { case 1:xianshi(); break; case 2:chaxun(); break; case 3:tianjia(); break; case 4:xiugai(); break; case 5:shanchu(); break; case 6:chuanjian(); break; case 7:chushihua(); break; case 8:shanchubiao(); break; default: break; } if(i==0) { printf("退出通讯录系统\n"); break; } } sqlite3_close(ppdb); return 0; }