2022-8-17 实现电子词典存入数据库中

电子词典的话需要自己找一份,存入一个文档中即可 

#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;
		}

	}



}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值