代码编织梦想

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

int i,j;

int main(int argc, const char *argv[])
{
	sqlite3 *db = NULL;
	if(sqlite3_open("./word.db",&db)!=SQLITE_OK)
	{
		fprintf(stderr,"__%d__ sqlite3_open failed\n",__LINE__);
		return -1;
	}
	printf("sqlite3_open success __%d__\n",__LINE__);

	//创建表格
	//数据库中怎么写,代码就怎么写
	char sql[128] = "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("table word create success __%d__\n",__LINE__);

	int fd = open("dict.txt",O_RDONLY);
	if(fd<0)
	{
		perror("open");
		return -1;
	}

	printf("%d\n",__LINE__);
	char buf;
	ssize_t res = -1;

	char auf[50]="";
	char word[50]="";
	char mean[50]="";
	i=0;j=0;	

	while(1)
	{
		bzero(&buf,1);
		res = read(fd,&buf,1);
		if(res < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res)
		{
			break;
		}

//		printf("%d\n",__LINE__);

		if(buf!='\n'&& buf!=' ')
		{
			auf[i++]=buf;
		}

		else if(buf == ' ' && j == 0)
		{
			auf[i]=0;
			strcpy(word,auf);
			i=0;
			j=1;
			bzero(auf,sizeof(auf));
		}

		else if(buf == '\n')
		{
			auf[i]=0;
			strcpy(mean,auf);
//			printf("%s \n",word);
//			printf("%s \n",mean);
			char sql[128] = "";
			sprintf(sql,"insert into word values ('%s','%s')",word,mean);
			char *errmsg = NULL;
			if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
			{
				fprintf(stderr,"__%d__ sqlite3_exec : %s\n",__LINE__,errmsg);
				return -1;
			}
			i=0;
			j=0;
			bzero(auf,sizeof(auf));
			bzero(word,sizeof(word));
			bzero(mean,sizeof(mean));
		}
	}


	if(sqlite3_close(db)!=SQLITE_OK)
	{
		fprintf(stderr,"__%d__ sqlite3_close failed\n",__LINE__);
		return -1;
	}
	printf("sqlite3_close success __%d__\n",__LINE__);

	return 0;
}

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_34177072/article/details/129676260