實現用資料庫儲存通訊錄 實現“增刪改查”的功能
阿新 • • 發佈:2019-02-02
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <string.h>
char name[20];
int age;
char sex[20];
char phonenumber[20];
void Menu(sqlite3* db);
void insert(sqlite3* db);
void delete(sqlite3* db);
void update(sqlite3* db);
void query(sqlite3* db);
void showscreen(sqlite3* db);
int main()
{
int ret;
sqlite3 *db;
char* errmsg;
ret = sqlite3_open("cmdaddress.db", &db);
if(ret != SQLITE_OK)
{
perror("open");
exit(1);
}
char sql_create[] = "create table if not exists stu(name text, sex text, age integer, phonenumber text);";
ret = sqlite3_exec(db, sql_create, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("create");
exit(2);
}
Menu(db);
showscreen(db);
ret = sqlite3_close(db);
if(ret != SQLITE_OK)
{
perror("close");
exit(1);
}
return 0;
}
void Menu(sqlite3* db)
{
int number;
printf("歡迎來到Coder通訊錄!\n");
printf("\n");
printf("1:新增聯絡人\n");
printf("\n");
printf("2:刪除聯絡人\n");
printf("\n");
printf("3:修改聯絡人資訊\n");
printf("\n");
printf("4:查詢通訊錄資訊\n");
printf("\n");
printf("5:彈出操作提示\n");
printf("\n");
printf("q:退出選單\n");
printf("\n");
printf("請輸入指令\n");
while(scanf("%d", &number) == 1)
{
switch(number)
{
case 1:
{
insert(db);
break;
}
case 2:
{
delete(db);
break;
}
case 3:
{
update(db);
break;
}
case 4:
{
query(db);
break;
}
case 5:
{
printf("1:新增聯絡人\n");
printf("\n");
printf("2:刪除聯絡人\n");
printf("\n");
printf("3:修改聯絡人資訊\n");
printf("\n");
printf("4:查詢通訊錄資訊\n");
printf("\n");
printf("q:退出選單\n");
printf("\n");
printf("請輸入數字\n");
break;
}
default: printf("請重新輸入正確的操作指令\n");
}
}
}
void insert(sqlite3* db)
{
int ret;
char *errmsg;
char sql_insert[1024] = {0};
printf("請輸入新增成員姓名:\n");
scanf("%s", name);
printf("請輸入成員性別:\n");
scanf("%s", sex);
printf("請輸入成員年齡:\n");
scanf("%d", &age);
printf("請輸入成員手機號碼:\n");
scanf("%s", phonenumber);
sprintf(sql_insert, "insert into stu values('%s', '%s', %d, '%s');", name, sex, age, phonenumber);
ret = sqlite3_exec(db, sql_insert, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("insert");
exit(1);
}
printf("操作成功\n");
memset(name, 0, 20);
memset(sex, 0, 20);
memset(phonenumber, 0, 20);
}
void delete(sqlite3* db)
{
char *errmsg;
int ret;
char sql_delete[1024] = {0};
int number;
printf("1.根據姓名進行刪除\n");
printf("\n");
printf("2.根據手機號碼進行刪除\n");
printf("\n");
printf("請輸入你想選擇的功能號碼\n");
scanf("%d", &number);
switch(number)
{
case 1:
{
printf("請輸入你想刪除的姓名\n");
scanf("%s", name);
sprintf(sql_delete, "delete from stu where name = '%s';", name);
ret = sqlite3_exec(db, sql_delete, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("delete");
exit(1);
}
printf("操作成功\n");
memset(name, 0, 20);
break;
}
case 2:
{
printf("請輸入你想刪除的手機號碼\n");
scanf("%s", phonenumber);
sprintf(sql_delete, "delete from stu where phonenumber = '%s';", phonenumber);
ret = sqlite3_exec(db, sql_delete, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("delete");
exit(1);
}
printf("操作成功\n");
memset(phonenumber, 0, 20);
break;
}
default: printf("請重新輸入正確的操作指令\n");
}
}
void update(sqlite3* db)
{
int ret;
char *errmsg;
char sql_update[1024] = {0};
char updatename[1024] = {0};
char updatesex[1024] = {0};
char updatephone[1024] = {0};
int number;
int updateage;
printf("1.根據姓名進行修改\n");
printf("\n");
printf("2.根據手機號碼進行修改\n");
printf("\n");
printf("請輸入你想選擇的功能號碼\n");
scanf("%d", &number);
switch(number)
{
case 1:
{
printf("請輸入你想修改的姓名\n");
scanf("%s", name);
printf("1.只修改姓名\n");
printf("\n");
printf("2.只修改性別\n");
printf("\n");
printf("3.只修改年齡\n");
printf("\n");
printf("4.只修改手機號碼\n");
printf("\n");
printf("請輸入你想選擇的功能號碼\n");
scanf("%d", &number);
switch(number)
{
case 1:
{
printf("請輸入姓名\n");
printf("\n");
scanf("%s", updatename);
sprintf(sql_update, "update stu set name = '%s' where name = '%s';", updatename, name);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update11");
exit(1);
}
printf("操作成功\n");
memset(name, 0, 20);
break;
}
case 2:
{
printf("請輸入性別\n");
printf("\n");
scanf("%s", updatesex);
sprintf(sql_update, "update stu set sex = '%s' where name = '%s';", updatesex, name);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update12");
exit(1);
}
printf("操作成功\n");
memset(name, 0, 20);
break;
}
case 3:
{
printf("請輸入年齡\n");
printf("\n");
scanf("%d", &updateage);
sprintf(sql_update, "update stu set age = %d where name = '%s';", updateage, name);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update13");
exit(1);
}
printf("操作成功\n");
memset(name, 0, 20);
break;
}
case 4:
{
printf("請輸入電話號碼\n");
printf("\n");
scanf("%d", updatephone);
sprintf(sql_update, "update stu set phonenumber = '%s' where name = '%s';", updatephone, name);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update14");
exit(1);
}
printf("操作成功\n");
memset(name, 0, 20);
break;
}
default: printf("請重新輸入正確的操作指令\n");
}
break;
}
case 2:
{
printf("請輸入你想修改的手機號碼\n");
scanf("%s", phonenumber);
printf("1.只修改姓名\n");
printf("\n");
printf("2.只修改性別\n");
printf("\n");
printf("3.只修改年齡\n");
printf("\n");
printf("4.只修改手機號碼\n");
printf("\n");
printf("請輸入你想選擇的功能號碼\n");
scanf("%d", &number);
switch(number)
{
case 1:
{
printf("請輸入姓名\n");
scanf("%s", updatename);
sprintf(sql_update, "update stu set name = '%s' where phonenumber = '%s';", updatename, phonenumber);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update21");
exit(1);
}
printf("操作成功\n");
memset(phonenumber, 0, 20);
break;
}
case 2:
{
printf("請輸入性別\n");
scanf("%s", updatesex);
sprintf(sql_update, "update stu set sex = '%s' where phonenumber = '%s';", updatesex, phonenumber);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update22");
exit(1);
}
printf("操作成功\n");
memset(phonenumber, 0, 20);
break;
}
case 3:
{
printf("請輸入年齡\n");
scanf("%d", updateage);
sprintf(sql_update, "update stu set age = %d where phonenumber = '%s';", updateage, phonenumber);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update23");
exit(1);
}
printf("操作成功\n");
memset(phonenumber, 0, 20);
break;
}
case 4:
{
printf("請輸入電話號碼\n");
scanf("%d", updatephone);
sprintf(sql_update, "update stu set phonenumber = '%s' where phonenumber = '%s';", updatephone, phonenumber);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update24");
exit(1);
}
printf("操作成功\n");
memset(phonenumber, 0, 20);
break;
}
}
break;
}
default: printf("請重新輸入正確的操作指令\n");
}
}
void query(sqlite3* db)
{
int ret, i, j;
char** result;
int row;
int column;
char *errmsg;
char sql_query[1024] = {0};
int number;
printf("1.根據姓名查詢\n");
printf("\n");
printf("2.根據手機號碼查詢\n");
printf("\n");
printf("請輸入你想選擇的功能號碼\n");
scanf("%d", &number);
switch(number)
{
case 1:
{
printf("請輸入姓名\n");
scanf("%s", name);
sprintf(sql_query, "select * from stu where name = '%s';", name);
ret = sqlite3_get_table(db, sql_query, &result, &row, &column, &errmsg);
if(ret != SQLITE_OK)
{
perror("query1");
exit(1);
}
for(i = 1; i <= row; i++)
{
for(j = 0; j < column; j++)
{
printf("%s ", result[i*column + j]);
}
printf("\n");
}
printf("操作成功\n");
memset(name, 0, 20);
break;
}
case 2:
{
printf("請輸入手機號碼\n");
scanf("%s", phonenumber);
sprintf(sql_query, "select * from stu where phonenumber = '%s';", phonenumber);
ret = sqlite3_get_table(db, sql_query, &result, &row, &column, &errmsg);
if(ret != SQLITE_OK)
{
perror("query2");
exit(1);
}
for(i = 1; i <= row; i++)
{
for(j = 0; j < column; j++)
{
printf("%s ", result[i*column + j]);
}
printf("\n");
}
printf("操作成功\n");
memset(phonenumber, 0, 20);
break;
}
default: printf("請重新輸入正確的操作指令\n");
}
}
void showscreen(sqlite3* db)
{
int i, j, ret;
char* errmsg;
int row;
int column;
char** result;
char sql_select[] = "select * from stu;";
ret = sqlite3_get_table(db, sql_select, &result, &row, &column, &errmsg);
if(ret != SQLITE_OK)
{
perror("showscreen");
exit(1);
}
for(i = 1; i <= row; i++)
{
for(j = 0; j < column; j++)
{
printf("%s ", result[i*column + j]);
}
printf("\n");
}
}
#include <stdlib.h>
#include <sqlite3.h>
#include <string.h>
char name[20];
int age;
char sex[20];
char phonenumber[20];
void Menu(sqlite3* db);
void insert(sqlite3* db);
void delete(sqlite3* db);
void update(sqlite3* db);
void query(sqlite3* db);
void showscreen(sqlite3* db);
int main()
{
int ret;
sqlite3 *db;
char* errmsg;
ret = sqlite3_open("cmdaddress.db", &db);
if(ret != SQLITE_OK)
{
perror("open");
exit(1);
}
char sql_create[] = "create table if not exists stu(name text, sex text, age integer, phonenumber text);";
ret = sqlite3_exec(db, sql_create, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("create");
exit(2);
}
Menu(db);
showscreen(db);
ret = sqlite3_close(db);
if(ret != SQLITE_OK)
{
perror("close");
exit(1);
}
return 0;
}
void Menu(sqlite3* db)
{
int number;
printf("歡迎來到Coder通訊錄!\n");
printf("\n");
printf("1:新增聯絡人\n");
printf("\n");
printf("2:刪除聯絡人\n");
printf("\n");
printf("3:修改聯絡人資訊\n");
printf("\n");
printf("4:查詢通訊錄資訊\n");
printf("\n");
printf("5:彈出操作提示\n");
printf("\n");
printf("q:退出選單\n");
printf("\n");
printf("請輸入指令\n");
while(scanf("%d", &number) == 1)
{
switch(number)
{
case 1:
{
insert(db);
break;
}
case 2:
{
delete(db);
break;
}
case 3:
{
update(db);
break;
}
case 4:
{
query(db);
break;
}
case 5:
{
printf("1:新增聯絡人\n");
printf("\n");
printf("2:刪除聯絡人\n");
printf("\n");
printf("3:修改聯絡人資訊\n");
printf("\n");
printf("4:查詢通訊錄資訊\n");
printf("\n");
printf("q:退出選單\n");
printf("\n");
printf("請輸入數字\n");
break;
}
default: printf("請重新輸入正確的操作指令\n");
}
}
}
void insert(sqlite3* db)
{
int ret;
char *errmsg;
char sql_insert[1024] = {0};
printf("請輸入新增成員姓名:\n");
scanf("%s", name);
printf("請輸入成員性別:\n");
scanf("%s", sex);
printf("請輸入成員年齡:\n");
scanf("%d", &age);
printf("請輸入成員手機號碼:\n");
scanf("%s", phonenumber);
sprintf(sql_insert, "insert into stu values('%s', '%s', %d, '%s');", name, sex, age, phonenumber);
ret = sqlite3_exec(db, sql_insert, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("insert");
exit(1);
}
printf("操作成功\n");
memset(name, 0, 20);
memset(sex, 0, 20);
memset(phonenumber, 0, 20);
}
void delete(sqlite3* db)
{
char *errmsg;
int ret;
char sql_delete[1024] = {0};
int number;
printf("1.根據姓名進行刪除\n");
printf("\n");
printf("2.根據手機號碼進行刪除\n");
printf("\n");
printf("請輸入你想選擇的功能號碼\n");
scanf("%d", &number);
switch(number)
{
case 1:
{
printf("請輸入你想刪除的姓名\n");
scanf("%s", name);
sprintf(sql_delete, "delete from stu where name = '%s';", name);
ret = sqlite3_exec(db, sql_delete, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("delete");
exit(1);
}
printf("操作成功\n");
memset(name, 0, 20);
break;
}
case 2:
{
printf("請輸入你想刪除的手機號碼\n");
scanf("%s", phonenumber);
sprintf(sql_delete, "delete from stu where phonenumber = '%s';", phonenumber);
ret = sqlite3_exec(db, sql_delete, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("delete");
exit(1);
}
printf("操作成功\n");
memset(phonenumber, 0, 20);
break;
}
default: printf("請重新輸入正確的操作指令\n");
}
}
void update(sqlite3* db)
{
int ret;
char *errmsg;
char sql_update[1024] = {0};
char updatename[1024] = {0};
char updatesex[1024] = {0};
char updatephone[1024] = {0};
int number;
int updateage;
printf("1.根據姓名進行修改\n");
printf("\n");
printf("2.根據手機號碼進行修改\n");
printf("\n");
printf("請輸入你想選擇的功能號碼\n");
scanf("%d", &number);
switch(number)
{
case 1:
{
printf("請輸入你想修改的姓名\n");
scanf("%s", name);
printf("1.只修改姓名\n");
printf("\n");
printf("2.只修改性別\n");
printf("\n");
printf("3.只修改年齡\n");
printf("\n");
printf("4.只修改手機號碼\n");
printf("\n");
printf("請輸入你想選擇的功能號碼\n");
scanf("%d", &number);
switch(number)
{
case 1:
{
printf("請輸入姓名\n");
printf("\n");
scanf("%s", updatename);
sprintf(sql_update, "update stu set name = '%s' where name = '%s';", updatename, name);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update11");
exit(1);
}
printf("操作成功\n");
memset(name, 0, 20);
break;
}
case 2:
{
printf("請輸入性別\n");
printf("\n");
scanf("%s", updatesex);
sprintf(sql_update, "update stu set sex = '%s' where name = '%s';", updatesex, name);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update12");
exit(1);
}
printf("操作成功\n");
memset(name, 0, 20);
break;
}
case 3:
{
printf("請輸入年齡\n");
printf("\n");
scanf("%d", &updateage);
sprintf(sql_update, "update stu set age = %d where name = '%s';", updateage, name);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update13");
exit(1);
}
printf("操作成功\n");
memset(name, 0, 20);
break;
}
case 4:
{
printf("請輸入電話號碼\n");
printf("\n");
scanf("%d", updatephone);
sprintf(sql_update, "update stu set phonenumber = '%s' where name = '%s';", updatephone, name);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update14");
exit(1);
}
printf("操作成功\n");
memset(name, 0, 20);
break;
}
default: printf("請重新輸入正確的操作指令\n");
}
break;
}
case 2:
{
printf("請輸入你想修改的手機號碼\n");
scanf("%s", phonenumber);
printf("1.只修改姓名\n");
printf("\n");
printf("2.只修改性別\n");
printf("\n");
printf("3.只修改年齡\n");
printf("\n");
printf("4.只修改手機號碼\n");
printf("\n");
printf("請輸入你想選擇的功能號碼\n");
scanf("%d", &number);
switch(number)
{
case 1:
{
printf("請輸入姓名\n");
scanf("%s", updatename);
sprintf(sql_update, "update stu set name = '%s' where phonenumber = '%s';", updatename, phonenumber);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update21");
exit(1);
}
printf("操作成功\n");
memset(phonenumber, 0, 20);
break;
}
case 2:
{
printf("請輸入性別\n");
scanf("%s", updatesex);
sprintf(sql_update, "update stu set sex = '%s' where phonenumber = '%s';", updatesex, phonenumber);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update22");
exit(1);
}
printf("操作成功\n");
memset(phonenumber, 0, 20);
break;
}
case 3:
{
printf("請輸入年齡\n");
scanf("%d", updateage);
sprintf(sql_update, "update stu set age = %d where phonenumber = '%s';", updateage, phonenumber);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update23");
exit(1);
}
printf("操作成功\n");
memset(phonenumber, 0, 20);
break;
}
case 4:
{
printf("請輸入電話號碼\n");
scanf("%d", updatephone);
sprintf(sql_update, "update stu set phonenumber = '%s' where phonenumber = '%s';", updatephone, phonenumber);
ret = sqlite3_exec(db, sql_update, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
perror("update24");
exit(1);
}
printf("操作成功\n");
memset(phonenumber, 0, 20);
break;
}
}
break;
}
default: printf("請重新輸入正確的操作指令\n");
}
}
void query(sqlite3* db)
{
int ret, i, j;
char** result;
int row;
int column;
char *errmsg;
char sql_query[1024] = {0};
int number;
printf("1.根據姓名查詢\n");
printf("\n");
printf("2.根據手機號碼查詢\n");
printf("\n");
printf("請輸入你想選擇的功能號碼\n");
scanf("%d", &number);
switch(number)
{
case 1:
{
printf("請輸入姓名\n");
scanf("%s", name);
sprintf(sql_query, "select * from stu where name = '%s';", name);
ret = sqlite3_get_table(db, sql_query, &result, &row, &column, &errmsg);
if(ret != SQLITE_OK)
{
perror("query1");
exit(1);
}
for(i = 1; i <= row; i++)
{
for(j = 0; j < column; j++)
{
printf("%s ", result[i*column + j]);
}
printf("\n");
}
printf("操作成功\n");
memset(name, 0, 20);
break;
}
case 2:
{
printf("請輸入手機號碼\n");
scanf("%s", phonenumber);
sprintf(sql_query, "select * from stu where phonenumber = '%s';", phonenumber);
ret = sqlite3_get_table(db, sql_query, &result, &row, &column, &errmsg);
if(ret != SQLITE_OK)
{
perror("query2");
exit(1);
}
for(i = 1; i <= row; i++)
{
for(j = 0; j < column; j++)
{
printf("%s ", result[i*column + j]);
}
printf("\n");
}
printf("操作成功\n");
memset(phonenumber, 0, 20);
break;
}
default: printf("請重新輸入正確的操作指令\n");
}
}
void showscreen(sqlite3* db)
{
int i, j, ret;
char* errmsg;
int row;
int column;
char** result;
char sql_select[] = "select * from stu;";
ret = sqlite3_get_table(db, sql_select, &result, &row, &column, &errmsg);
if(ret != SQLITE_OK)
{
perror("showscreen");
exit(1);
}
for(i = 1; i <= row; i++)
{
for(j = 0; j < column; j++)
{
printf("%s ", result[i*column + j]);
}
printf("\n");
}
}