-
导入后检查解释意思是否完整,双词性
-
中间有空格的单词
-
o'clock
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/types.h>
int main(int argc, const char *argv[])
{
//打开数据库
sqlite3* db = NULL;
if(sqlite3_open("./word.db", &db) != SQLITE_OK)
{
printf("err_code:%d\n", sqlite3_errcode(db));
printf("errmsg:%s\n", sqlite3_errmsg(db));
fprintf(stderr, "__%d__ sqlite3_open failed\n", __LINE__);
return -1;
}
printf("sqlite3_open success\n");
//打开文件
FILE *fd = fopen("./dict.text","r");
if(NULL == fd)
{
perror("fail to open");
exit(1);
}
//创建一个表格
char* sql = "create table if not exists word ( word char, mean char);" ;
char* errmsg = NULL;
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
printf("create table success\n");
int i=1;
char buf[128];
char mean[128];
char word[128];
char *s;
while(1)
{
bzero(buf,sizeof(buf));
bzero(word,sizeof(word));
bzero(mean,sizeof(mean));
s=fgets(buf,128,fd);
if(NULL == s)
{
break;
}
char *errmsg=NULL;
char *q=buf;
strcpy(word, strtok(q," "));
strcpy(mean, strtok(NULL,"\n"));
char copy[128]= "";
strcpy(copy,mean+2);
char *p = strtok(copy," ");
while(*p !='\0')
{
if(*p > 0 && *p<127)
{
p++;
}
else
{
break;
}
}
if(*p == '\0')
{
word[strlen(word)]=' ';
strcat(word,strtok(mean," "));
strcpy(mean,strtok(NULL,"\r"));
}
/* while(*mean == ' ')
{
++mean;
}
*/
char sql[128]="";
sprintf(sql, "insert into word values (\"%s\",\"%s\");",word,mean );
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
}
//关闭数据库
if(sqlite3_close(db) != SQLITE_OK)
{
printf("err_code:%d\n", sqlite3_errcode(db));
printf("errmsg:%s\n", sqlite3_errmsg(db));
fprintf(stderr, "__%d__ sqlite3_close failed\n", __LINE__);
return -1;
}
printf("sqlite3_close success\n");
fclose(fd);
return 0;
}