作业-11.29

将txt中的单词转到数据库中

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

void do_insert(sqlite3* db, int id, char word[], char jieshi[]);
void txt_todatabase(sqlite3* db);

int main(int argc, const char *argv[])
{
    if(2 != argc)
    {
        printf("please %s <database.db>\n", argv[0]);
        return -1;
    }
    //创建并打开数据库
    sqlite3 *db = NULL;
    if(sqlite3_open(argv[1], &db) != SQLITE_OK)
    {
        fprintf(stderr, "line:%d--sqlite3_open:%s\n", __LINE__, sqlite3_errmsg(db));
        return -1;
    }
    printf("database open success\n");

    //创建表格
    char sql[128] = "create table if not exists direct (id int primary key, word char, jieshi 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;
    }
    printf("create table success \n");

    //将txt中的单词转化为数据库的数据
    txt_todatabase(db);

    //关闭数据库
    if(sqlite3_close(db) != SQLITE_OK)
    {
        fprintf(stderr, "line:%d sqlite3_open:%s\n", __LINE__, sqlite3_errmsg(db));
        fprintf(stderr, "line:%d sqlite3_open:%d\n", __LINE__, sqlite3_errcode(db));
        return -1;
    }
    printf("database close success\n");

    return 0;
}

void txt_todatabase(sqlite3* db)
{
    char buff[300];//在文件中获取一行
    char word[64];//存放单词
    char jieshi[256];//存放解释
    int id = 0;

    FILE *fp = fopen("dict.txt", "r");
    char *p = NULL;

    while(NULL != fgets(buff, sizeof(buff), fp))
    {

        //解析出单词和解释
        p = buff;
        while(1)
        {
            if(*p == ' ')
            {
                if(*(p+1) == ' ')
                {
                    break;
                }
            }
            p++;
        }
        *p = '\0';
        p++;

        //截取单词
        strcpy(word, buff);

        //跳过中间的若干个 空格
        while(*p == ' ')
        {
            p++;
        }
        //截取解释
        strcpy(jieshi, p);
        
        //将数据写入数据库中
        id++;
        do_insert(db, id, word, jieshi);
    }

}

void do_insert(sqlite3* db, int id, char word[], char jieshi[])
{
    char sql[600] = "";
    char* errmsg = NULL;
    sprintf(sql, "insert into direct values (%d,\"%s\",\"%s\")", id, word, jieshi);
    if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        fprintf(stderr, "line:%d-- sqlite3_exec:%s\n", __LINE__, errmsg);
        return;
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
07-13 10:08:02.266 403 12194 V EmulatedCamera_FakeCamera3: threadLoop: ReadoutThread: Timed out waiting for request 07-13 10:08:02.266 403 12194 V EmulatedCamera_FakeCamera3: threadLoop: ReadoutThread waiting for request 07-13 10:08:02.268 401 12178 I modules.usbaudio.audio_hal: in_read ret 0 07-13 10:08:02.273 401 12178 I modules.usbaudio.audio_hal: in_read ret 0 07-13 10:08:02.276 403 12194 V EmulatedCamera_FakeCamera3: threadLoop: ReadoutThread: Timed out waiting for request 07-13 10:08:02.277 403 12194 V EmulatedCamera_FakeCamera3: threadLoop: ReadoutThread waiting for request 07-13 10:08:02.278 401 12178 I modules.usbaudio.audio_hal: in_read ret 0 07-13 10:08:02.286 716 12304 E ActivityManager: ANR in com.winew.ddcsx (com.winew.ddcsx/.activity.MainActivity) 07-13 10:08:02.286 716 12304 E ActivityManager: PID: 11129 07-13 10:08:02.286 716 12304 E ActivityManager: Reason: Input dispatching timed out (1942a1b com.winew.ddcsx/com.winew.ddcsx.activity.MainActivity (server) is not responding. Waited 5002ms for MotionEvent(deviceId=11, source=0x00001002, displayId=0, action=DOWN, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, classification=NONE, edgeFlags=0x00000000, xPrecision=8.5, yPrecision=15.2, xCursorPosition=nan, yCursorPosition=nan, pointers=[0: (1365.8, 1340.9)]), policyFlags=0x62000000) 07-13 10:08:02.286 716 12304 E ActivityManager: Parent: com.winew.ddcsx/.activity.MainActivity 07-13 10:08:02.286 716 12304 E ActivityManager: Load: 12.42 / 11.29 / 12.0 07-13 10:08:02.286 716 12304 E ActivityManager: ----- Output from /proc/pressure/memory ----- 07-13 10:08:02.286 716 12304 E ActivityManager: some avg10=0.00 avg60=0.00 avg300=0.00 total=249698 07-13 10:08:02.286 716 12304 E ActivityManager: full avg10=0.00 avg60=0.00 avg300=0.00 total=74789 07-13 10:08:02.286 716 12304 E ActivityManager: ----- End output from /proc/pressure/memory -----
07-15

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值