导入词表 :
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//创建并打开数据库
sqlite3 *db=NULL;
if(sqlite3_open("./my.db",&db)!=SQLITE_OK)
{
fprintf(stderr,"line:%d sqlite3_open:%s\n",__LINE__,sqlite3_errmsg(db));
return -1;
}
printf("database open success\n");
//创建一个表格
char sql[128]="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,"line:%d sqlite3_exec:%s\n",__LINE__,errmsg);
return -1;
}
printf("create table success\n");
//导入单词
FILE *fd=fopen("./111.txt","r");
char res[512]="";
char word[32]="";
char mean[256]="";
char *p=NULL;
while(fgets(res,100,fd)!=NULL)
{
p=res;
while(*p!=' '||*(p+1)!=' ')
{
p++;
}
*p='\0';
strcpy(word,res);
strcpy(mean,p+3);
sprintf(sql,"insert into word values(\"%s\",\"%s\")",word,mean);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"line:%d sqlite3_exec:%s\n",__LINE__,errmsg);
return -1;
}
}
printf("插入成功\n");
//关闭数据库
if(sqlite3_close(db)!=SQLITE_OK)
{
fprintf(stderr,"line:%d sqlite3_close:%s\n",__LINE__,sqlite3_errmsg(db));
fprintf(stderr,"line:%d sqlite3_close:%s\n",__LINE__,sqlite3_errmsg(db));
return -1;
}
printf("database close success\n");
return 0;
}