电子词典的话需要自己找一份,存入一个文档中即可
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
//int do_insert (sqlite3 *db,int num);
int mystrtok(char *dest,int i)
{
char *ch=dest;
while(*ch!='\0')
{
if(*ch==' '&& *(ch+1)==' ')
{
return i;
}
i++;
ch=dest+i;
}
}
int main(int argc, const char *argv[])
{
//打开需要读取内容的文件
// int fd=open("./dict.txt",O_RDONLY);
//打开数据库
sqlite3* db = NULL;
if(sqlite3_open("./my.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");
//执行sql语句
#if 0
int sqlite3_exec(
sqlite3* db, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *arg, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
#endif
//创建一个表格
char* sql = "create table if not exists stu (English_word char , Chinese_translation 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");
char buf[128]="";
char *arr;
char *brr;
char crr[128]="";
char *p;
// char *p=flag;
int a=0;
int i=0;
//将dict.txt中的英文单词,和中文翻译写入到数据库中
//先打开文件
//然后以行为单位,\n作为分隔符一行一行读取
//存入时单词和翻译中间隔了3个空格
//打开需要读取内容的文件
FILE *fd=fopen("./dict.txt","r");
while(1)
{
// res=read(fd,buf,sizeof(buf)-1);
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),fd);
if(strlen(buf) == 0)
{
break;
}
i=mystrtok(buf,a);
*p=buf[i+2];
// puts(p);
strcpy(p,";");
arr=strtok(buf,";");
brr=strtok(NULL,"\r");
sprintf(crr,"%s(\"%s\",'%s');","insert into stu values",arr,brr);
if(sqlite3_exec(db, crr, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
}
}