2022.8.17 作业

1、将dict.txt中的内容导入到数据库中

        注意:

                a.导入后检查解释意思是否完整,双词性

                b.中间有空格的单词

                c. o'clock

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

#define ERR_MSG(msg1,msg2) do{\
	fprintf(stderr,"__%s__ __%d__\n",__func__,__LINE__);\
	fprintf(stderr,"%s:%s\n",msg1,msg2);\
}while(0)

int do_insert(sqlite3 *db,char *word,char *annotation);
int do_deal(char *buf);

int main(int argc, const char *argv[])
{
	FILE *fd=NULL;

	sqlite3 *db=NULL;
	if(sqlite3_open("./net_words.db",&db)!=SQLITE_OK){
		ERR_MSG("sqlite3_open",sqlite3_errmsg(db));
		return -1;
	}
	printf("sqlite3_open success\n");

	char *sql="create table if not exists words (word char,annotation char)";
	char *errmsg=NULL;
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK){
		ERR_MSG("sqlite3_exec",errmsg);
	}
	printf("sqlite3_exec success\n");

	fd=fopen("./dict.txt","r");
	if(fd==NULL){
		perror("fopen");
		return -1;
	}
	printf("fopen success\n");

	char buf[64]={0};
	int index=0;
	while(1){
		bzero(buf,sizeof(buf));
		if(fgets(buf,sizeof(buf),fd)==NULL){
			break;
		}
		buf[strlen(buf)-1]='\0';
		index=do_deal(buf);
		buf[index]='\0';
		do_insert(db,buf,buf+index+3);
	}

	printf("insert finish\n");

	if(sqlite3_close(db)!=SQLITE_OK){
		ERR_MSG("sqlite3_close",sqlite3_errmsg(db));
	}
	printf("sqlite3_close success\n");

	fclose(fd);

	return 0;
}

int do_insert(sqlite3 *db,char *word,char *annotation)
{
	char buf[128]={0};
	char *errmsg=NULL;
	sprintf(buf,"insert into words values (\"%s\",\"%s\")",word,annotation);
	if(sqlite3_exec(db,buf,NULL,NULL,&errmsg)!=SQLITE_OK){
		fprintf(stderr,"word=%s\n",word);
		fprintf(stderr,"annotation=%s\n",annotation);
		ERR_MSG("insert",errmsg);
		return -1;
	}
	return 0;
}

int do_deal(char *buf)
{
	int i=0;
	char *ch=buf+i;
	while(*ch!='\0'){
		if(*ch==' ' && *(ch+1)==' '){
			return i;
		}
		i++;
		ch=buf+i;
	}
	return 0;
}

测试:

o'clock可以写入数据库:

单词之间由空格也可以写入数据库:

双词性的单词也可以写入数据库:

 缩写单词也可以写入数据库:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值