单词词典入库

main.c

#include<stdio.h>
#include<sqlite3.h>
#include<fcntl.h>
#include<string.h>
void callback(){

}
int main(int argc, const char *argv[])
{
	sqlite3* db;
	if(sqlite3_open("./mysq.db",&db)!=SQLITE_OK){
		printf("sql open error: %s\n",sqlite3_errmsg(db));
		return -1;
	}
	char* errmsg;
	char *sql;
	sql="create table if not exists dict(id INTEGER PRIMARY KEY AUTOINCREMENT,name text,explain text);";
	int res = sqlite3_exec(db,sql,0,NULL,&errmsg);
	if(res!=SQLITE_OK){
		fprintf(stderr,"sql open error: %s\n",sqlite3_errmsg(db));
		return -1;
	}
	FILE *fd = fopen("./dict.txt","r");
	if(fd<0){
		perror("open");
		return -1;
	}
	char buf_val[128]="",buf_value[128]="",str[250]="";
	char *scanf_res;
	int res_insert;
	char readc;
	int i=0,j=0,flag=0;
	while(1){
		sql=NULL;
		flag =0;
		scanf_res = fgets(str,sizeof(str),fd);
		i=0;j=0;
		while(1){
			if(*(str+i)=='\n')
			{
				break;
			}
			if(0==flag)
			{
				if(*(str+i)==32){
					flag=1;
				}else{
					*(buf_val+i)=*(str+i);
				}
			}else{
				*(buf_value+j)=*(str+i);
				j++;
			}
			i++;
		}	
		if(scanf_res==NULL){
			printf("into success\n");
			break;
		}
		sql=sqlite3_mprintf("insert into dict(name,explain)values('%q','%q');",buf_val,buf_value);
		res_insert = sqlite3_exec(db,sql,0,0,&errmsg);
		if(res_insert!=SQLITE_OK){
			printf("sqlerr: %s\n",errmsg);
			sqlite3_free(errmsg);
			break;
		}
		bzero(buf_val,sizeof(buf_val));
		bzero(buf_value,sizeof(buf_value));
	}
	if(fclose(fd)<0)
	{
		perror("close");
		return -1;
	}
	if(sqlite3_close(db)!=SQLITE_OK){
		fprintf(stderr,"sql open error: %s\n",sqlite3_errmsg(db));
		return -1;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值