这个感觉比较简单些,因为直接调用sql语句就好了,我看着别人的代码理解了一遍,自己敲了一遍
参考 -----> https://blog.csdn.net/jiaqi_327/article/details/20799277
#include
#include
#include
sqlite3 *db = NULL;
void welcome()
{
printf("1.查看通讯录\n");
printf("2.增加联系人\n");
printf("3.删除联系人\n");
printf("4.修改联系人信息\n");
printf("5.查找联系人\n");
}
static int callback(void * data, int argc, char * * argv, char * * name)
{
int i;
for(i = 0; i < argc; i++)
{
printf("%s = %s\n",name[i],argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
void show()
{
char *errmsg;
char *sql = NULL;
int ret;
sql = "select * from contact;";
ret = sqlite3_exec(db,sql,callback,NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("显示失败!%s\n",errmsg);
sqlite3_free(errmsg);
}
}
void add()
{
char *errmsg = 0;
char *sql = NULL;
int ret;
char *name;
char *age;
char *relation;
char *number;
name = (char *)malloc(sizeof(char)*10);
age = (char *)malloc(sizeof(char)*11);
relation = (char *)malloc(sizeof(char)*10);
number = (char *)malloc(sizeof(char)*11);
sql = (char *)malloc(sizeof(char)*100);
printf("请输入联系人的姓名:\n");
scanf("%s",name);
printf("请输入联系人的年龄:\n");
scanf("%s",age);
printf("请输入与联系人的关系:\n");
scanf("%s",relation);
printf("请输入联系人的电话:\n");
scanf("%s",number);
sprintf(sql,"insert into contact values('%s','%s','%s','%s');",name,age,relation,number);
ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("添加失败!%s",errmsg);
sqlite3_free(errmsg);
}
free(name);
free(relation);
free(number);
free(sql);
}
void delete()
{
char *errmsg = 0;
char *sql = NULL;
int ret;
char *name;
name = (char *)malloc(sizeof(char)*10);
sql = (char *)malloc(sizeof(char)*64);
printf("请输入你要删除的联系人:");
scanf("%s",name);
sprintf(sql,"delete from contact where name ='%s'",name);
ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("删除失败!%s\n",errmsg);
sqlite3_free(errmsg);
}
free(name);
free(sql);
}
void update()
{
char *errmsg;
char *sql = NULL;
int ret;
char *name;
char *number;
name = (char *)malloc(sizeof(char)*10);
number = (char *)malloc(sizeof(char)*11);
sql = (char *)malloc(sizeof(char)*64);
printf("请输入你要修改的联系人姓名:");
scanf("%s",name);
printf("请输入你修改的电话号码:");
scanf("%s",number);
sprintf(sql,"update contact set number = '%s' where name = '%s'",number,name);
ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("修改失败!%s\n",errmsg);
sqlite3_free(errmsg);
}
free(name);
free(number);
free(sql);
}
void search()
{
char *errmsg;
char *name;
char *sql;
int ret;
printf("请输入你要查询的联系人的名字!\n");
scanf("%s",name);
name = (char *)malloc(sizeof(char)*10);
sql = (char *)malloc(sizeof(char)*64);
sprintf(sql,"select * from contact where name = '%s'",name);
ret = sqlite3_exec(db,sql,callback,NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("查询失败!%s\n",errmsg);
sqlite3_free(errmsg);
}
free(name);
free(sql);
}
int main(void)
{
char *errmsg;
int ret;
int num;
char *sql= NULL;
ret = sqlite3_open("contact.db",&db);
if(ret)
{
printf("不能打开数据库!%s\n",sqlite3_errmsg(db));
return -1;
}
sql = (char*)malloc(sizeof(char)*64);
sql = "create table contact(name text,age text,relation text,number text);";
ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("表已经存在!\n");
}
do
{
welcome();
printf("请输入序号:");
scanf("%d",&num);
switch (num)
{
case 1:show();break;
case 2:add();break;
case 3:delete();break;
case 4:update();break;
case 5:search();break;
}
}while(num);
return 0;
}