1、将dict.txt中的内容导入到数据库中
注意:
a.导入后检查解释意思是否完整,双词性
b.中间有空格的单词
c. o'clock
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#define ERR_MSG(msg1,msg2) do{\
fprintf(stderr,"__%s__ __%d__\n",__func__,__LINE__);\
fprintf(stderr,"%s:%s\n",msg1,msg2);\
}while(0)
int do_insert(sqlite3 *db,char *word,char *annotation);
int do_deal(char *buf);
int main(int argc, const char *argv[])
{
FILE *fd=NULL;
sqlite3 *db=NULL;
if(sqlite3_open("./net_words.db",&db)!=SQLITE_OK){
ERR_MSG("sqlite3_open",sqlite3_errmsg(db));
return -1;
}
printf("sqlite3_open success\n");
char *sql="create table if not exists words (word char,annotation char)";
char *errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK){
ERR_MSG("sqlite3_exec",errmsg);
}
printf("sqlite3_exec success\n");
fd=fopen("./dict.txt","r");
if(fd==NULL){
perror("fopen");
return -1;
}
printf("fopen success\n");
char buf[64]={0};
int index=0;
while(1){
bzero(buf,sizeof(buf));
if(fgets(buf,sizeof(buf),fd)==NULL){
break;
}
buf[strlen(buf)-1]='\0';
index=do_deal(buf);
buf[index]='\0';
do_insert(db,buf,buf+index+3);
}
printf("insert finish\n");
if(sqlite3_close(db)!=SQLITE_OK){
ERR_MSG("sqlite3_close",sqlite3_errmsg(db));
}
printf("sqlite3_close success\n");
fclose(fd);
return 0;
}
int do_insert(sqlite3 *db,char *word,char *annotation)
{
char buf[128]={0};
char *errmsg=NULL;
sprintf(buf,"insert into words values (\"%s\",\"%s\")",word,annotation);
if(sqlite3_exec(db,buf,NULL,NULL,&errmsg)!=SQLITE_OK){
fprintf(stderr,"word=%s\n",word);
fprintf(stderr,"annotation=%s\n",annotation);
ERR_MSG("insert",errmsg);
return -1;
}
return 0;
}
int do_deal(char *buf)
{
int i=0;
char *ch=buf+i;
while(*ch!='\0'){
if(*ch==' ' && *(ch+1)==' '){
return i;
}
i++;
ch=buf+i;
}
return 0;
}
测试:
o'clock可以写入数据库:
单词之间由空格也可以写入数据库:
双词性的单词也可以写入数据库:
缩写单词也可以写入数据库: