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;
}
相关推荐
Q_970956391 分钟前
java+vue+SpringBoo校园失物招领网站(程序+数据库+报告+部署教程+答辩指导)
java·数据库·vue.js
Wyc724099 分钟前
Maven
java·数据库·maven
程序猿小D12 分钟前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站
羊小猪~~17 分钟前
数据库学习笔记(十七)--触发器的使用
数据库·人工智能·后端·sql·深度学习·mysql·考研
背太阳的牧羊人1 小时前
Neo4j 的向量搜索(Neo4jVector)和常见的向量数据库(比如 Milvus、Qdrant)之间的区别与联系
数据库·neo4j·milvus
liulun1 小时前
在浏览器中使用SQLite(官方sqlite3.wasm)
数据库·sqlite·wasm
IT项目管理2 小时前
达梦数据库DMHS介绍及安装部署
linux·数据库
你都会上树?2 小时前
MySQL MVCC 详解
数据库·mysql
大春儿的试验田3 小时前
高并发收藏功能设计:Redis异步同步与定时补偿机制详解
java·数据库·redis·学习·缓存
Ein hübscher Kerl.3 小时前
虚拟机上安装 MariaDB 及依赖包
数据库·mariadb