SQLite3实现简单的联系人管理系统
有关sqlite3的基础知识请点击:SQLite3的使用
效果展示:
创建一个名为contacts.db的数据库
首先,我们需要创建一个名为contacts.db的数据库,并建立一个名为"contact"的表,用于存储联系人信息。该表包含四个列:id、name、phone和email。
以下是创建数据表的SQL语句:
CREATE TABLE IF NOT EXISTS contact (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL,phone TEXT,email TEXT
);
代码:
#include <sqlite3.h>
#include <stdio.h>#define DATABASE_NAME "contacts.db"int main(int argc, char* argv[]) {sqlite3 *db;char *zErrMsg = 0;int rc;/*打开数据库*/rc = sqlite3_open(DATABASE_NAME, &db);if (rc) {fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));sqlite3_close(db);return 1;} else {fprintf(stdout, "Opened database successfully\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 successfully\n");}/*关闭数据库文件*/sqlite3_close(db);return 0;
}
实现“添加联系人”功能的代码:
#include <sqlite3.h>
#include <stdio.h>#define DATABASE_NAME "contacts.db"
#define STRLEN 50int main(int argc, char* argv[]) {sqlite3 *db;char *zErrMsg = 0;int rc;/*打开数据库*/rc = sqlite3_open(DATABASE_NAME, &db);if (rc) {fprintf(stderr, "无法打开数据库: %s\n", sqlite3_errmsg(db));sqlite3_close(db);return 1;}char *sql_query, name[STRLEN], phone[STRLEN], email[STRLEN];int opt;/*创建表的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 successfully\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\t电话号码是: %s\n电子邮件是: %s\n", name, phone, email);printf("请输入[y]确认 \n");if('y' != getchar()) {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://查找联系人break;case 3://更新联系人break;case 4://删除联系人break;case 5:sqlite3_close(db);return 1;break;default:printf("无效的选项,请重新输入!\n");break;}}/*关闭数据库文件*/sqlite3_close(db);return 0;
}
完整代码
联系人管理系统完整代码
#include <sqlite3.h>
#include <stdio.h>#define DATABASE_NAME "contacts.db"
#define STRLEN 50static 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, char* argv[]) {sqlite3 *db;char *zErrMsg = 0;int rc;/*打开数据库*/rc = sqlite3_open(DATABASE_NAME, &db);if (rc) {fprintf(stderr, "无法打开数据库: %s\n", sqlite3_errmsg(db));sqlite3_close(db);return 1;}char *sql_query, name[STRLEN], phone[STRLEN], email[STRLEN];int id, opt;/*创建表的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 successfully\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\t电话号码是: %s\n电子邮件是: %s\n", name, phone, email);printf("请输入[y]确认 \n");if('y' != getchar()) {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); // sqlite3_mprintf函数会自动申请内存空间,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;
}
上述代码实现了一个简单的联系人管理系统,可以通过命令行进行添加、查找、更新和删除操作。其中,使用sqlite3_mprintf()函数来创建SQL查询语句。
在程序开始时,会提示用户输入要执行的操作,并根据用户输入的选项来执行相应的操作。每个操作都有相应的提示信息和输入要求,例如添加联系人需要输入姓名、电话号码和电子邮件地址等信息。
在查询联系人时,我们使用回调函数callback()来处理查询结果并将输出内容改为中文。对于其他操作,我们只需检查SQL查询是否成功并处理相应的错误消息即可。
需要注意的是,在实际开发中,还需要对输入进行合法性验证以及防止SQL注入攻击等安全问题。