1
1.1【sqlite3_open】
1.1.1函数原型
【int sqlite3_open(const char *filename, /* Database filename (UTF-8) */sqlite3 **ppDb /* OUT: SQLite db handle */);】
1.1.2函数功能
打开数据库文件(创建一个数据库连接)
1.1.3函数参数
【filename】:数据库文件路径
【ppDb】:操作数据库指针存放空间的首地址
1.1.4返回值
【成功】:返回【SQLITE_OK】
【失败】:返回错误码
1.2【sqlite3_close】
1.2.1函数原型
【int sqlite3_close(sqlite3 *);】
1.2.2函数功能
关闭数据库连接
1.2.3函数参数
1.2.4返回值
1.3【sqlite3_exec】
1.3.1函数原型
【int sqlite3_exec(sqlite3*, /* An open database */const char *sql, /* SQL to be evaluated */int (*callback)(void*,int,char**,char**), /* Callback function */void *, /* 1st argument to callback */char **errmsg/* Error msg written here */);】
1.3.2函数功能
执行一条SQL语句
1.3.3函数参数
【sqlite3*】:数据库句柄
【sql】:要执行的SQL语句字符串首地址
【callback】:只有在select时才会使用,其余调用时传递【NULL】
【void*】:给回调函数的传参
【errmsg】:出错信息存放空间首地址(使用完毕后使用sqlite3_free释放空间)
1.3.4返回值
【成功】:返回【SQLITE_OK】
【失败】:返回错误码
源码示例(1):
typedef struct student
{char name[32];char sex[8];int age;
}stu_t;int main(void)
{sqlite3 *pdb = NULL;int ret = 0;char cmdbuf[1024] = {0};char *perrmsg = NULL;stu_t s = {"张三", "男", 18};ret = sqlite3_open("student.db", &pdb);if (ret != SQLITE_OK){fprintf(stderr, "sqlite3_open failed:%s\n", sqlite3_errmsg(pdb));return -1;}sprintf(cmdbuf, "create table if not exists student (id integer primary key asc, name varchar(255), sex varchar(32), age integer);");ret = sqlite3_exec(pdb, cmdbuf, NULL, NULL, &perrmsg);if (ret != SQLITE_OK){fprintf(stderr, "sqlite3_exec failed:%s\n", perrmsg);sqlite3_free(perrmsg);sqlite3_close(pdb);return -1;}sprintf(cmdbuf, "insert into student values (NULL, '%s', '%s', %d);", s.name, s.sex, s.age);ret = sqlite3_exec(pdb, cmdbuf, NULL, NULL, &perrmsg);if (ret != SQLITE_OK){fprintf(stderr, "sqlite3_exec failed:%s\n", perrmsg);sqlite3_free(perrmsg);sqlite3_close(pdb);return -1;}sqlite3_close(pdb);return 0;
}
源码示例(2):
/*练习:将一张单词表中单词及其含义插入到数据库中*/
int LoadDictToDB(void)
{sqlite3 *pdb = NULL;char cmdbuf[4096] = {0};int ret = 0;char *perrmsg = NULL;FILE *fp = NULL;char tmpbuff[4096] = {0};char *pret = NULL;char *ptmpword = NULL;char *ptmpmean = NULL;long len = 0;long curlen = 0;ret = sqlite3_open("dict.db", &pdb);if (ret != SQLITE_OK){fprintf(stderr, "fail to sqlite3_open:%s\n", sqlite3_errmsg(pdb));return -1;}sprintf(cmdbuf, "create table if not exists dict (编号 integer primary key asc, 单词 varchar(64), 含义 varchar(4096));");ret = sqlite3_exec(pdb, cmdbuf, NULL, NULL, &perrmsg);if (ret != SQLITE_OK){fprintf(stderr, "fail to sqlite3_exec:%s\n", perrmsg);sqlite3_free(perrmsg);sqlite3_close(pdb);return -1;}fp = fopen("dict.txt", "r");if (NULL == fp){perror("fail to fopen");return -1;}fseek(fp, 0, SEEK_END);len = ftell(fp);rewind(fp);while (1){pret = fgets(tmpbuff, sizeof(tmpbuff), fp);if (NULL == pret){break;}curlen = ftell(fp);printf("已加载 %.2lf%%\r", (double)curlen / (double)len * 100);fflush(stdout);ptmpword = strtok(tmpbuff, " ");ptmpmean = strtok(NULL, "\r");sprintf(cmdbuf, "insert into dict values (NULL, \"%s\", \"%s\");", ptmpword, ptmpmean);ret = sqlite3_exec(pdb, cmdbuf, NULL, NULL, &perrmsg);if (ret != SQLITE_OK){fprintf(stderr, "fail to sqlite3_exec:%s\n", perrmsg);sqlite3_free(perrmsg);sqlite3_close(pdb);return -1;}}sqlite3_close(pdb);return 0;
}int main(void)
{if (access("dict.db", F_OK)){LoadDictToDB();}return 0;
}