电子词典上传数据库

#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>

sqlite3 * open_sql(void);
int exec_create(sqlite3 *db);
int close_sql(sqlite3 * db);
int do_insert(sqlite3 *db);
int main(int argc, const char *argv[])
{
	//打开数据库
	sqlite3 *db=open_sql();
	//创建表格
   exec_create(db);
     //导入单词表数据
	 
  do_insert(db);

	//关闭数据库
   close_sql(db);
	return 0;
}

sqlite3 * open_sql(void )
{
	sqlite3 *db=NULL;
    if(sqlite3_open("./my.db",&db)!=SQLITE_OK)
	{
		fprintf(stderr,"line:%d,sqlite3_open:%s",__LINE__ \
				,sqlite3_errmsg(db));
		fprintf(stderr,"line:%d,sqlite3_open:%d",__LINE__\
				,sqlite3_errcode(db));
	}
	return db;

}

int exec_create(sqlite3 *db)
{
	char *errmsg=NULL;
	char sql[128]="create  table if not exists wordexc (name char,stat char);";
	if(sqlite3_exec(db,sql,NULL, NULL,&errmsg)!=SQLITE_OK)
	{
		fprintf(stderr,"line:%d,sqlite3_exec:%s",__LINE__\
				,errmsg);
	}
	return 0;

}

int close_sql(sqlite3 * db)
{
	if(sqlite3_close(db)!=SQLITE_OK)
	{
		fprintf(stderr,"line:%d,sqlite3_close:%s",__LINE__\
				,sqlite3_errmsg(db));
		fprintf(stderr,"line:%d,sqlite3_close:%d",__LINE__\
				,sqlite3_errcode(db));
	}
	return 0;

}

int do_insert(sqlite3 *db)
{
	FILE *fd=fopen("./dict.txt","r");
	fseek(fd,0, SEEK_SET);
	if(fd==NULL)
	{
      perror("fopen");
	}
	char wd_name[128]="";
    
	char wd_stat[128]="";

	char wd_trs[128]="";	
	char temp[128]="";	
	while(1)
	{
	bzero(temp,sizeof(temp));	
	if(NULL == fgets(temp,sizeof(temp),fd))
	{
		printf("上传完毕\n");
		break;
	}
	temp[strlen(temp)-1]=0;
	char *p=temp;
	int len=0;
	int len_stat=0;
	int len_t=0;
	int len_a=0;
	int flag=0;
	for(p=temp;p<temp+strlen(temp);p++)
	{
		    len++;
	/*
        if((*p==' ')&&(*(p+1)!=' '))
		{
			flag++;
		   continue;
		}
		*/  		
        if(*p==' ')
		{
		  flag++;
		  if((flag==1)&&(*(p+1)==' '))
		  {
	      bzero(wd_name,sizeof(wd_name));	
          strncpy(wd_name,temp,len-1); 
		  }
		  if((flag==2)&&(*(p+1)!=' '))
		  {
	       bzero(wd_name,sizeof(wd_name));	
          strncpy(wd_name,temp,len-1); 
		  }
		  if((flag>2)&&(*(p+1)!=' '))
		  {
           len_t=len; 
		  }
		}
	}
	       bzero(wd_stat,sizeof(wd_stat));	
          strncpy(wd_stat,temp+len_t,len-len_t+1); 
	/*
	char *p=wd_stat;
	for(p=wd_stat;p<wd_stat+strlen(wd_stat);p++)
	{
        if(*p=='.')
		{
			break;
		}
	}
	
	bzero(wd_trs,sizeof(wd_trs));
     strcpy(wd_tr,p);
	 printf("%s\n",temp);
	 printf("%s\n",wd_name);
	 printf("%s\n",wd_stat);
	 printf("%s\n",wd_trs);*/
	char sql[128]="";
	char *errmsg=NULL;
	sprintf(sql,"INSERT INTO wordexc VALUES (\"%s\", \"%s\");",wd_name,wd_stat);
	if(sqlite3_exec(db,sql,NULL, NULL,&errmsg)!=SQLITE_OK)
	{
		fprintf(stderr,"line:%d,sqlite3_exec:%s",__LINE__\
				,errmsg);
	}
	}
	fclose(fd);
	return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值