sqlite3将词典导入数据库

使用sqlite3代码实现将词典导入数据库中

cpp 复制代码
#include <head.h>
#include <sqlite3.h>
#include <strings.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
	 sqlite3 *db = NULL;
    if(sqlite3_open("./dict.db",&db) != SQLITE_OK)
    {
        fprintf(stdin,"sqlite3_open:%s %d",\
                sqlite3_errmsg(db),sqlite3_errcode(db));
        return -1;
    }
    printf("sqlite3_open success\n");

    char sql_c[128] = "create table if not exists Dictionary (word char,TRANSLATORS char)";
    char *errmsg = NULL;
    if(sqlite3_exec(db,sql_c,NULL,NULL,&errmsg) != SQLITE_OK)
    {
        fprintf(stderr,"sqlite3_exec:%s %d",errmsg,sqlite3_errcode(db));                       
        return -1;
    }
    printf("create table Dictionary success\n");

	FILE * fd = fopen("./dict.txt","r");
	if(fd < 0)
	{
		ERRO_MES("open");
		return -1;
	}
	char buf[128]  = "";
	char word[128] = "";
	char mean[128] = "";
	char sql_i[128]  = "";
	while(1)
	{
		if(fgets(buf,sizeof(buf),fd) == NULL)
		{
			break;
		}
		buf[strlen(buf)-1] = 0;
		for(int i=0;i<strlen(buf)-2;i++)
		{
			if(buf[i] != ' ' && buf[i+1] == ' ')
			{
				strncpy(word,buf,i+1);
			}
			else if(buf[i] == ' '&& buf[i+1] == ' ' &&buf[i+2]!= ' ')
			{
				strcpy(mean,(buf+i+2));
				break;
			}
		}
		bzero(sql_i,sizeof(sql_i));
		sprintf(sql_i,"insert into Dictionary values ('%s','%s')",word,mean);
		if(sqlite3_exec(db,sql_i,NULL,NULL,&errmsg) != SQLITE_OK)
		{
			fprintf(stderr,"sqlite3_exec:%s %d",errmsg,sqlite3_errcode(db));                       
			return -1;
		}

	}


     if(sqlite3_close(db) != SQLITE_OK)
     {
         fprintf(stdin,"sqlite3_close:%s %d",\
                 sqlite3_errmsg(db),sqlite3_errcode(db));
         return -1;
     }
	 printf("sqlite3_close success\n");
	 fclose(fd);
	return 0;
}
相关推荐
黑客笔记1 小时前
sql注入漏洞的对抗
数据库·sql·测试工具
tianlebest1 小时前
Laravel 安全:批量赋值 fillable 与 guarded
数据库·安全·laravel
小叶子来了啊1 小时前
软考(信息系统运行管理员)
数据库
chunfeng—1 小时前
Redis 主从同步与对象模型(四)
数据库·redis·缓存·集群·哨兵
Leo.yuan2 小时前
热力图是什么?三分钟学会热力图数据分析怎么做!
大数据·数据库·数据挖掘·数据分析·html
张哈大2 小时前
【 Redis | 实战篇 缓存 】
数据库·redis·笔记·spring·缓存
玩转数据库管理工具FOR DBLENS2 小时前
项目高压生存指南:科学重构身体与认知系统的抗压算法
大数据·数据库·职场和发展·项目管理
正在走向自律3 小时前
【金仓数据库征文】学校AI数字人:从Sql Server到KingbaseES的数据库转型之路
数据库·人工智能·kingbasees·金仓数据库 2025 征文·数据库平替用金仓
_extraordinary_3 小时前
MySQL 索引(一)
android·数据库·mysql
gjc5923 小时前
MySQL OCP试题解析(2)
android·数据库·mysql·开闭原则