#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;
}
电子词典上传数据库
最新推荐文章于 2024-06-19 18:15:00 发布