代码如下
#include<stdio.h>
#include<sqlite3.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
int do_insert(sqlite3 *db);
void do_select(sqlite3* db);
int main(int argc, const char *argv[])
{
//打开数据库
sqlite3 *db=NULL;
if(sqlite3_open("./dict.db",&db)!=SQLITE_OK)
{
printf("err_code:%d\n",sqlite3_errcode(db));
printf("err_msg:%s\n",sqlite3_errmsg(db));
fprintf(stderr,"__%d__sqlite3_open failed\n",__LINE__);
return -1;
}
printf("sqlite3_open success\n");
//执行sql语句
char *sql="create table if not exists dict (word char ,translate 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("sqlite3_exce success\n");
//菜单和选择
char choose=0;
while(1)
{
system("clear");
printf("---------------------------\n");
printf("--------1. 插入数据--------\n");
printf("--------2. 查询数据--------\n");
printf("--------3. 退出------------\n");
printf("---------------------------\n");
printf("请输入选项>>>");
choose = getchar();
while(getchar()!=10);
switch(choose)
{
case '1':
do_insert(db); //插入数据
break;
case '2':
do_select(db); //显示数据库数据
break;
case '3':
goto END;
break;
default:
printf("输入错误,请重新输入\n");
}
printf("输入任意字符清屏>>>");
while(getchar()!=10);
}
END:
//关闭数据库
if(sqlite3_close(db)!=SQLITE_OK)
{
printf("err_code:%d\n",sqlite3_errcode(db));
printf("err_msg:%s\n",sqlite3_errmsg(db));
fprintf(stderr,"__%d__sqlite3_close failed\n",__LINE__);
return -1;
}
printf("sqlite3_close success\n");
return 0;
}
int do_insert(sqlite3 *db)
{
char word[50]=""; //存储单词
char translate[50]; //存储翻译
char temp[50]; //存储临时的文字段
//已读的方式打开文件
FILE *fp=fopen("./dict.txt","r");
if(fp==NULL)
{
fprintf(stderr,"line:%d ",__LINE__);
perror("fopen");
return -1;
}
int res=0; //为EOF(即-1)时结束
while(1)
{
int sflag=0;
bzero(word,sizeof(word));
bzero(translate,sizeof(translate));
while(1)
{
bzero(temp,sizeof(temp));
//读取有效文字段
res=fscanf(fp,"%s",temp);
if(res==-1) //判断fscanf是否出错,以及文件是否读完;
{
if(errno==0)
{
fclose(fp);
printf("字典传输完成\n");
return 0;
}
else
{
fprintf(stderr,"line:%d ",__LINE__);
perror("fscanf");
}
}
int len=strlen(temp);
//判断单词是否到翻译了
if(sflag==0)
{
for(int i=0;i<len;i++)
{
if(temp[i]=='.')
{
sflag=1;
break;
}
}
}
//写入不同的字符串(单词和翻译)
if(sflag==0)
{
strcat(word,temp);
strcat(word," ");
}
else if(sflag==1)
{
strcat(translate,temp);
strcat(translate," ");
}
char x;
while(1)
{
x=fgetc(fp);
if(x==' '||x==10)
break;
}
if(x==10)
break;
}
//执行sql语句
char sql[128];
sprintf(sql,"insert into dict values (\"%s\",\"%s\");",word,translate);
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("sqlite3_insert success\n");
}
//关闭文件
fclose(fp);
return 0;
}
int callback(void* arg,int columns,char **columns_text ,char **columns_name)
{
int i=0;
while(*(int*)arg==0)
{
for(i=0;i<columns;i++)
{
printf("%s\t\t",*(columns_name+i));
}
printf("\n");
*(int*)arg=1;
}
for(i=0;i<columns;i++)
{
printf("%s\t\t",*(columns_text+i));
}
printf("\n");
return 0; //成功返回0,失败返回非0;
}
void do_select(sqlite3* db)
{
//执行sql语句
char sql[128];
sprintf(sql,"select *from dict ;");
char *errmsg=NULL;
int flag=0;
if(sqlite3_exec(db,sql,callback,(void*)&flag,&errmsg)!=SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return ;
}
printf("sqlite3_select success\n");
}
结果