#include <myhead.h> #define ERR_MSG(msg) do {\ fprintf(stderr, "__%d__", __LINE__);\ perror(msg);\ } while (0) int do_insert(sqlite3 *db, char *en, char *ch); int main(int argc, const char *argv[]) { //创建并打开数据库 sqlite3 *db = NULL; if (sqlite3_open("./dict.db", &db) != SQLITE_OK) { fprintf(stderr, "sqlite3 open failed: __%d__\n", __LINE__); return -1; } //创建表格 char sql[128] = "create table if not exists dict (\ word char not null,\ mean char not null);"; char *errmsg = NULL; if (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) { fprintf(stderr, "sqlite3_exec: %s __%d__\n", errmsg, __LINE__); return -1; } //打开本地文件 FILE *fp; if ((fp = fopen("./dict.txt", "r")) == NULL) { ERR_MSG("fopen"); return -1; } char buf[128] = ""; char *res; while (1) { bzero(buf, sizeof(buf)); res = fgets(buf, sizeof(buf), fp); if (res == NULL) { break; } //获取单词和释义 char *en = buf; char *temp = buf; while (*temp++ != 32); if (*(temp - 1) == 32) { while (*temp++ != 32); } *temp = '\0'; temp++; char *ch = temp; //添加到数据库中 do_insert(db, en, ch); } printf("更新数据库done\n"); //关闭文件描述符 if (fclose(fp) < 0) { ERR_MSG("fclose"); return -1; } return 0; } int do_insert(sqlite3 *db, char *en, char *ch) { char sql[128] = ""; sprintf(sql, "insert into dict (word, mean) values (\"%s\", \"%s\");", en, ch); char *errmsg = NULL; if (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) { fprintf(stderr, "sqlite3_exec: %s __%d__\n", errmsg, __LINE__); return -1; } return 0; }
07-12
5738
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-26
9400
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交