打通文件与数据库的最后一堵墙----将文件导入数据库之中

将文件导入到数据库中

数据库使用的轻量级sqlite3数据库

以简单的词典为例,增加了行数,方便比对执行结果

#include <errno.h>
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DATABASE "my.db"

int main(int argc, const char* argv[])
{
    char str[512] = { 0 }; //储存单词以及解释

    char word[64] = { 0 }; // sqlite3储存单词本身
    char translation[256] = { 0 }; // sqlite3储存单词解释
    sqlite3* db;
    FILE* fp;
    // 打开数据库文件
    if (SQLITE_OK != sqlite3_open(DATABASE, &db)) {
        perror("sqlist error");
        exit(-1);
    }
    //打开目标文件
    if (NULL == (fp = fopen("dict.txt", "r"))) {
        perror("fopen error");
        exit(-1);
    }

    char sqlstr[512] = { 0 };

    //组装sql语句 创建表
    sprintf(sqlstr, "CREATE TABLE IF NOT EXISTS dict (word TEXT,translation TEXT)");
    printf("create table sql :[%s]\n", sqlstr);
    // 执行sql语句
    if (SQLITE_OK != sqlite3_exec(db, sqlstr, NULL, NULL, NULL)) {
        printf("error: %s\n", sqlite3_errmsg(db));
        printf("表已经存在\n");
    } else {
        printf("表创建成功\n");
    }
    int count = 0; //用于计数
    while (NULL != (fgets(str, sizeof(str), fp))) { //循环获取单词及解释
        // fegts遇到换行符会停止
        memset(word, 0, sizeof(word));
        int i = 0;
        char* p = str;
        //将每行的'\n'替换为'\0'
        str[strlen(str) - 1] = '\0';
        //打印看现象 方便代码调试
        // printf("------str=[%s]-------\n", str);

        while (*p != ' ') { //找到单词所在位置
            word[i] = *p; //将单词储存在word数组中
            p++;
            i++;
        }
        word[i] = '\0'; //将单词后的第一个空格替换为'\0'
        p++; //当前已经是'\0'了 移动到下一个空格的位置
        while (*p == ' ' && *p != '\0') { //截取出单词解释部分 储存到str数组中
            p++;
        }
        //执行到这里 word中储存的就是单词本身 str中储存的是单词解释

        // 处理两个字段中的单引号 如 one's  将其转换为 one.s 再插入
        // 因为在组装sql语句时 %s 也使用了单引号 ''
        char* temp = p; //将指针p所指向的解释临时指向指针temp 保证原解释不被破坏
        //找解释中的 '
        while (*temp != '\0') {
            if (*temp == '\'') { //找到 '  使用\' 将'转义
                *temp = '.'; //替换为 .
            }
            temp++; //直到找到所有 '
        }
        //找单词中的 '
        temp = word;
        while (*temp != '\0') {
            if (*temp == '\'') { //找到 '  使用\' 将'转义
                *temp = '.'; //替换为 .
            }
            temp++; //直到找到所有 '
        }
        
        strcpy(translation, p); //将处理过的单词解释复制到字段translation中
        translation[strlen(translation) - 1] = '\0';

        // printf("insert count = [%d]\tword:[%s]\t\ttranslation:[%s]\n", count, word, translation);
         printf("insert count = [%d]\tword:[%s]\t\ttranslation:[%s]\n", count, word, translation);

        //向数据库中插入数据
        sprintf(sqlstr, "INSERT INTO dict  VALUES('%s','%s')", word, translation);

        // printf("insert sql %s\n", sqlstr);
        if (SQLITE_OK != sqlite3_exec(db, sqlstr, NULL, NULL, NULL)) {
            printf("sql error: %s\n", sqlite3_errmsg(db));
            exit(-1);
        }
        count++;
        memset(str, 0, sizeof(str));
    }

    printf("sum count =%d,process end .........\n", count);
    return 0;
}

原词典数据:
在这里插入图片描述

程序执行结果:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值