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

 

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

  2. 中间有空格的单词

  3. o'clock

#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/types.h>



int main(int argc, const char *argv[])
{
	//打开数据库
	sqlite3* db = NULL;
	if(sqlite3_open("./word.db", &db) != SQLITE_OK)
	{
		printf("err_code:%d\n", sqlite3_errcode(db));
		printf("errmsg:%s\n", sqlite3_errmsg(db));
		fprintf(stderr, "__%d__ sqlite3_open failed\n", __LINE__);
		return -1;
	}
	printf("sqlite3_open success\n");


	//打开文件
	FILE *fd = fopen("./dict.text","r");
	if(NULL == fd)
	{
		perror("fail to open");
		exit(1);
	}


	//创建一个表格
	char* sql = "create table if not exists word ( word char, mean char);" ;
	char* errmsg = NULL;

	if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
	{
		fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
		return -1;
	}
	printf("create table success\n");


	int i=1;
	char buf[128];
	char mean[128];
	char word[128];

    char *s;
	while(1)
	{
		bzero(buf,sizeof(buf));
		bzero(word,sizeof(word));
		bzero(mean,sizeof(mean));
		s=fgets(buf,128,fd);
		if(NULL == s)
		{
			break;
		}
		char *errmsg=NULL;


		char *q=buf;
		strcpy(word, strtok(q," "));

		strcpy(mean, strtok(NULL,"\n"));
        char copy[128]= "";
		strcpy(copy,mean+2);

		char *p = strtok(copy," ");


		while(*p !='\0')
		{
			if(*p > 0 && *p<127)
			{
				p++;
			}
			else
			{
				break;
			}
		}
		if(*p == '\0')
		{
			word[strlen(word)]=' ';
			strcat(word,strtok(mean," "));
			strcpy(mean,strtok(NULL,"\r"));
		}



	/*	while(*mean == ' ')
		{

			++mean;
		}
		
		*/
			
		


		char sql[128]="";
		sprintf(sql, "insert into word values (\"%s\",\"%s\");",word,mean );
			if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
		{
			fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
			return -1;
		}

	}
	//关闭数据库
	if(sqlite3_close(db) != SQLITE_OK)
	{
		printf("err_code:%d\n", sqlite3_errcode(db));
		printf("errmsg:%s\n", sqlite3_errmsg(db));
		fprintf(stderr, "__%d__ sqlite3_close failed\n", __LINE__);
		return -1;

	}
	printf("sqlite3_close success\n");

	fclose(fd);



	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值