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;
}
相关推荐
我命由我123452 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list
-SGlow-6 小时前
MySQL相关概念和易错知识点(2)(表结构的操作、数据类型、约束)
linux·运维·服务器·数据库·mysql
明月5667 小时前
Oracle 误删数据恢复
数据库·oracle
♡喜欢做梦8 小时前
【MySQL】深入浅出事务:保证数据一致性的核心武器
数据库·mysql
遇见你的雩风8 小时前
MySQL的认识与基本操作
数据库·mysql
dblens 数据库管理和开发工具8 小时前
MySQL新增字段DDL:锁表全解析、避坑指南与实战案例
数据库·mysql·dblens·dblens mysql·数据库连接管理
weixin_419658318 小时前
MySQL的基础操作
数据库·mysql
不辉放弃9 小时前
ZooKeeper 是什么?
数据库·大数据开发
Goona_10 小时前
拒绝SQL恐惧:用Python+pyqt打造任意Excel数据库查询系统
数据库·python·sql·excel·pyqt
程序员编程指南10 小时前
Qt 数据库连接池实现与管理
c语言·数据库·c++·qt·oracle