华清远见第五课程day6作业

将文本批量写入数据库

#include<myhead.h>

int main(int argc, const char *argv[])
{
	char word[128];
	char mean[128];
	sqlite3 *db;
	if(sqlite3_open("./sq.db",&db)!=SQLITE_OK){
		fprintf(stderr,"sqlite3_open:%s %d __%d__\n",\
				sqlite3_errmsg(db),sqlite3_errcode(db), __LINE__);
		return -1;
	}
	printf("sqlite3_open success\n");
	char sql[128]="create table if not exists dict (word char,mean char)";
	char* errmsg = NULL;
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK){
		fprintf(stderr,"sqlite3_exec:%s  __%d__\n",errmsg,__LINE__);
		return -1;
	}
	printf("create table stu success\n");


	FILE *fp;
	if((fp=fopen("dict.txt","r"))==NULL){
		perror("fopen error");
		return 0;
	}
	char temp[128];
	while(1){
		int ret = fscanf(fp,"%s %s",word,mean);
		
		if(ret ==-1){
			break;
		}
		while(1){
			fscanf(fp,"%s",temp);
			if((temp[1]=='.'&&temp[2]<150)||temp[2]=='.'||temp[3]=='.'){
				strcat(mean,temp);
			}
			else{
				fseek(fp,-strlen(temp),SEEK_CUR);
				break;
			}
		}
		bzero(temp,sizeof(temp));
		int i,k;
		i=k=0;
		for( i;i<strlen(word);i++){
			temp[k]=word[i];
			if(word[i]=='\''){
				k++;
				temp[k]=word[i];
			}
			k++;
		}
		
		printf("%s   %s\n",temp,mean);
		sprintf(sql,"insert into dict values('%s','%s');",temp,mean);
		if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK){
			fprintf(stderr,"sqlite3_exec:%s  __%d__\n",errmsg,__LINE__);
			return -1;
		}
		bzero(sql,sizeof(sql));
		bzero(word,sizeof(word));
		bzero(mean,sizeof(mean));

	}


	if(sqlite3_close(db)!=SQLITE_OK){
		fprintf(stderr,"sqlite3_close:%s %d __%d__",\
				sqlite3_errmsg(db),sqlite3_errcode(db),__LINE__);
		return -1;
	}
	printf("sqlite3_close success\n");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值