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;
}
相关推荐
RestCloud13 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud13 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
佛祖让我来巡山14 小时前
深入理解JVM内存分配机制:大对象处理、年龄判定与空间担保
jvm·内存分配·大对象处理·空间担保·年龄判定
ClouGence15 小时前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
杨杨杨大侠20 小时前
打开 JVM 黑匣子——走进 Java 字节码(一)
java·jvm·agent
DemonAvenger1 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
J2K2 天前
JDK都25了,你还没用过ZGC?那真得补补课了
java·jvm·后端
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql