词典导入数据库

#include <head.h>
int main(int argc, const char *argv[])
{
    sqlite3 *db = NULL;
    if (sqlite3_open("sql.db", &db) != SQLITE_OK)
    {
        fprintf(stderr, "line:%d sqlite3_open:%s\n", __LINE__, sqlite3_errmsg(db));
        return -1;
    }
    char sql[128] = "create table if not exists dics (word_s char,explain_s char);";
    char *errmsg = NULL;
    if (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        fprintf(stderr, "line:%d sqlite3_exec:%s\n", __LINE__, errmsg);
        return -1;
    }
    FILE *fd = fopen("dict.txt", "r");
    if (fd == NULL)
        PRINT_ERR("fopen");
    fseek(fd, 0, SEEK_END);
    int fileLen = ftell(fd);
    fseek(fd, 0, SEEK_SET);
    char word[40];
    char explain[60];
    char buf[100];
    int i;
    int count = 0;
    while (1)
    {
        if (fgets(buf, sizeof(buf), fd) == NULL)
            break;
        count+=strlen(buf);
        buf[strlen(buf) - 1] = 0;
        for (i = 0; i < strlen(buf); i++)
        {
            if (buf[i] == ' ' && buf[i + 1] == ' ')
            {
                buf[i] = 0;
                strcpy(word, buf);
                break;
            }
        }
        for (int j = i + 1; j < strlen(buf) + i + 1; j++)
        {
            if (buf[j] != ' ')
            {
                strcpy(explain, buf + j);
                break;
            }
        }
        sprintf(sql, "INSERT INTO dics VALUES (\"%s\",\"%s\");", word, explain);
        if (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
        {
            fprintf(stderr, "line:%d sqlite3_exec:%s\n", __LINE__, errmsg);
            return -1;
        }
        printf("读取中..%%%.1f\r", (float)count /(fileLen/(100)));
        fflush(stdout);
    }
    sqlite3_close(db);
    printf("录入成功\n");
    return 0;
}

过程要10秒左右
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值