9.4网络编程---day7

1.将dict.txt导入到数据库中

单词一列,意思一列

9.4网络编程---day7_第1张图片

#include 

void do_insert(sqlite3 *,char *,char *);

int main(int argc, const char *argv[])
{
    FILE *srcfp;
    if((srcfp = fopen("./dict.txt", "r")) == NULL)
    {
        perror("srcfile open error");
        return -1;
    }

	if(access("./cd.db",F_OK)==0)
	{
		system("rm ./cd.db");
	}


	sqlite3 *db=NULL;
	if(sqlite3_open("./cd.db",&db)!=0)
	{
		fprintf(stderr,"sqlite3_open %d:%s__%d__\n",\
				sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);
	}

	char sql[128]="create table if not exists stu (单词 char,中文解释 char);";
	char *errmsg=NULL;
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
	{
		fprintf(stderr,"sqlite3_exec %s__%d__\n",errmsg,__LINE__);
	}
	printf("create table success\n");


    //定义搬运工
    char buf;
	char dc[30]="";
	char js[128]="";
	int i=0,j=0,k=0,flag=0;
	while((buf=fgetc(srcfp)) != EOF)
	{
		if(buf=='\n')
		{
			flag=0;
			
			//插入
			do_insert(db,dc,js);


			bzero(dc,sizeof(dc));
			bzero(js,sizeof(js));
			i=0,j=0;
		}
		if(buf!='\n')
		{
			if(buf==' ')
				flag=1;
			if(0==flag&&buf!=' ')
			{
				dc[i]=buf;
				i++;
			}else if(1==flag&&buf!=' ')
			{
				js[j]=buf;
				j++;
			}
		}

    }
	printf("create insert success\n");
    //关闭文件指针
    fclose(srcfp);

	return 0;
}

void do_insert(sqlite3 *db,char *cd,char *js)
{
	char sql[128]="";
	char *errmsg=NULL;
	sprintf(sql,"insert into stu values (\"%s\",\"%s\");",cd,js);
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
	{
		fprintf(stderr,"sqlite3_exec %s__%d__\n",errmsg,__LINE__);
	}
}

思维导图:

9.4网络编程---day7_第2张图片

你可能感兴趣的:(网络)