1、项目介绍
本项目以sqlite3为基本框架完成一个简易的手机联系人管理系统,用户可以根据自己需要进行添加联系人、删除联系人、更新联系人、查找联系人以及退出等。
2、本项目涉及到的sqlite3 API
①sqlite3_open() 函数用于打开一个 SQLite 数据库文件的函数,这个函数使用指定的数据库文件名打开一个新的数据库连接,并且把连接句柄返回给调用者如果连接成功,将会返回SQLITE_OK编码(0),否则将会返回其他错误代码
②sqlite3_close()函数用于关闭指定数据库连接的函数,成功关闭返回 SQLITE_OK 编码(0),否则返回其他错误代码
③sqlite3_exec()` 函数用于在 SQLite 数据库连接上执行一条或多条 SQL 语句,并调用一个回调函数处理执行结果。
3、运行代码
标签:zErrMsg,管理系统,db,rc,联系人,简易版,printf,sqlite3 From: https://blog.csdn.net/qq_54967229/article/details/139634248#include <stdio.h>
#include <sqlite3.h>#define BASENAME "contacts.db"
#define STRLEN 1024static int callback(void *NotUsed,int argc,char **argv,char **azColName){
int i;
printf("查询结果\n");
for(i=0; i<argc; i++){
printf("%s:%s\n",azColName[i],argv[i]?argv[i]:"NULL");
}
printf("\n");
return 0;
}int main(int argc,const char *argv[]){
sqlite3 *db;
char *zErrMsg = 0;
int rc,opt,id;
char * sql_query,name[STRLEN],phone[STRLEN],email[STRLEN];
//打开数据库
rc = sqlite3_open(BASENAME,&db);
if(rc != SQLITE_OK){
fprintf(stderr,"Database can't open:%s\n",sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}else{
printf("This Database is successfull open\n");
}//创建表的SQL语句
char *sql = "CREATE TABLE IF NOT EXISTS contact("
"id integer primary key autoincrement,"
"name text not null,"
"phone text,"
"email text"
");";
//执行SQL语句
rc = sqlite3_exec(db,sql,0,0,&zErrMsg);
if(rc != SQLITE_OK){
fprintf(stderr,"SQL ERROR:%s\n",zErrMsg);
sqlite3_free(zErrMsg);
}else{
printf("Table created sucessfull\n");
}
while(1){
//提示语句
printf("请输入你想做的操作序号\n");
printf("1-->添加联系人\n");
printf("2-->查找联系人\n");
printf("3-->更新联系人\n");
printf("4-->删除联系人\n");
printf("5-->退出\n");
//获取用户输入的操作序号
while(!scanf("%d",&opt)) getchar();
switch(opt){
case 1:
//添加联系人
printf("请输入姓名:\n");
scanf("%s",name);
getchar();
printf("请输入联系电话:\n");
scanf("%s",phone);
getchar();
printf("请输入电子邮件:\n");
scanf("%s",email);
getchar();
printf("您输入的姓名为:%s,联系电话为:%s,电子邮件为:%s\n",name,phone,email);
printf("请输入Y确认\n");
if(getchar() != 'Y'){
while(getchar() != '\n');
printf("添加联系人失败,已取消\n");
continue;
}
sql_query = sqlite3_mprintf("INSERT INTO contact"
"(name,phone,email) VALUES('%s','%s','%s')",
name,phone,email);
rc = sqlite3_exec(db,sql_query,NULL,NULL,&zErrMsg);
if(rc != SQLITE_OK){
printf("添加联系人失败[%s]\n",zErrMsg);
sqlite3_free(zErrMsg);
}else{
printf("添加联系人成功!\n");
}
sqlite3_free(sql_query);
break;
case 2:
printf("请输入您要查找的姓名:\n");
scanf("%s",name);
getchar();sql_query = sqlite3_mprintf("SELECT *FROM contact WHERE name LIKE '%s'",name);
rc = sqlite3_exec(db,sql_query,callback,NULL,&zErrMsg);
if(rc != SQLITE_OK){
printf("查询联系人失败:%s\n",zErrMsg);
sqlite3_free(zErrMsg);
}
break;
case 3:
printf("请输入要更新的联系人ID:\n");
while(!scanf("%d",&id)) getchar();
printf("请输入新的姓名:\n");
scanf("%s",name);
getchar();
printf("请输新的入联系电话:\n");
scanf("%s",phone);
getchar();
printf("请输入新的电子邮件:\n");
scanf("%s",email);
getchar();sql_query = sqlite3_mprintf("UPDATE contact SET name ='%s',phone ='%s',email = '%s'WHERE id = %d",name,phone,email,id);
rc = sqlite3_exec(db,sql_query,NULL,NULL,&zErrMsg);
if(rc != SQLITE_OK){
printf("更新联系人失败:%s\n",zErrMsg);
sqlite3_free(zErrMsg);
}else{
printf("更新成功\n");
}
break;
case 4:
printf("请输入要删除人的ID:\n");
while(!scanf("%d",&opt)) getchar();sql_query = sqlite3_mprintf("DELETE FROM contact WHERE id = %d",id);
rc = sqlite3_exec(db,sql_query,NULL,NULL,&zErrMsg);
if(rc != SQLITE_OK){
printf("删除联系人失败:%s\n",zErrMsg);
sqlite3_free(zErrMsg);
}else{
printf("删除成功\n");
}
break;
case 5:
sqlite3_close(db);
return 1;
break;
default:
printf("您输入的是无效序号,请重新输入!\n");
break;
}
}
//关闭数据库
sqlite3_close(db);
return 0;
}