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 小时前
★CentOS:MySQL数据备份
数据库·mysql·adb
SelectDB2 小时前
5000+ 中大型企业首选的 Doris,在稳定性的提升上究竟花了多大的功夫?
大数据·数据库·apache
路多辛2 小时前
Golang database/sql 包深度解析(二):连接池实现原理
数据库·sql·golang
SimonKing2 小时前
Mybatis批量插入,形式不同性能也不同
数据库·后端·程序员
稚辉君.MCA_P8_Java2 小时前
豆包 Java的23种设计模式
java·linux·jvm·设计模式·kubernetes
杰克尼3 小时前
MYSQL-175. 组合两个表
数据库·mysql
DemonAvenger3 小时前
MySQL索引原理深度解析与优化策略实战
数据库·mysql·性能优化
189228048614 小时前
NY270NY273美光固态闪存NY277NY287
服务器·网络·数据库·科技·性能优化
星霜笔记7 小时前
Docker 部署 MariaDB+phpMyAdmin+Nextcloud 完整教程
运维·数据库·docker·容器·mariadb
wyiyiyi13 小时前
【Web后端】Django、flask及其场景——以构建系统原型为例
前端·数据库·后端·python·django·flask