3.20作业

head.h

#ifndef __HEAD_H__
#define __HEAD_H__
#include <sqlite3.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <pthread.h>
#include <sys/select.h>
#include <sys/time.h>
#include <poll.h>
#include <sys/un.h>

#define ERR_MSG(msg) do{\
    fprintf(stderr,"line:%d ",__LINE__);\
    perror(msg);\
}while(0)
#define ERR_MSG_1(msg) ERR_MSG(msg);return -1

#define IF_RET_ERR(ret,fi,msg) do{\
    if (ret fi)\
    {\
        ERR_MSG_1(msg);\
    }\
}while(0)
#define IF_RET_ERR_0(ret,msg) IF_RET_ERR(ret,<0,msg)

#define IF_SQL_EXEC_ERR(ret) do{\
    if (ret !=SQLITE_OK)\
    {\
        fprintf(stderr,"line:%d %s\n",__LINE__,errmsg);\
        return -1;\
    }\
}while(0)

#define SUCCESS(msg) printf(msg " success __%d__\n",__LINE__)
#endif

main.c

#include "head.h"

int main(int argc, const char *argv[])
{
    //创建并打开数据库
    sqlite3 *db=NULL;
    if(sqlite3_open("./mydict.db",&db)!=SQLITE_OK)
    {
        fprintf(stderr,"__%d__sqlite3_open failed\n",__LINE__);
    }
    SUCCESS("sqlite3_open");

    //创建表格
    char sql[128]="create table if not exists dict(word char,trans char)";
    char * errmsg;
    IF_SQL_EXEC_ERR(sqlite3_exec(db,sql,NULL,NULL,&errmsg));
    SUCCESS("table stu create");

    FILE* fp=fopen("./dict.txt","r");
    IF_RET_ERR(fp,==NULL,"fopen");
    char buf[128];
    char word[128];
    char *trans=NULL;

    while (1)
    {

        bzero(buf,sizeof(buf));
        bzero(word,sizeof(word));
        if(fgets(buf,sizeof(buf),fp)==NULL)
        {
            break;
        }
        sscanf(buf,"%s",word);
        trans=buf;
LOOP:
        while(*trans++!=' ');
        if(*trans==' ')
        {
            trans+=2;
        }
        else
        {
            goto LOOP;
        }

        //printf("%s:%s",word,translation);
        bzero(sql,sizeof(sql));
        sprintf(sql,"insert into dict values(\"%s\",\"%s\")",word,trans);
        //printf("%s\n",sql);

        IF_SQL_EXEC_ERR(sqlite3_exec(db,sql,NULL,NULL,&errmsg));
    }

END:
    //关闭数据库
    if(sqlite3_close(db)!=SQLITE_OK)
    {
        fprintf(stderr,"__%d__sqlite3_close failed\n",__LINE__);
    }
    fclose(fp);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值