sqlite的文件导入操作

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <errno.h>
#include <sqlite3.h>

#define LOG(s) printf("[%s] {%s:%d} %s \n", __DATE__, __FILE__, __LINE__, s);

int do_insert(sqlite3* db);
int do_select(sqlite3* db);

int main(int argc, char *argv[])
{

	//打开数据库
	sqlite3* db;
	if(sqlite3_open("./dict.db", &db) != SQLITE_OK)
	{
		LOG("sqlite3_open error");
		return -1;
	}
	printf("sqlite3_open success\n");

	char sql[128] = "create table if not exists dict(word char, mean char);";
	char* errmsg = NULL;
	if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
	{
		LOG("sqlite3_exec error");
		return -1;
	}

	int choose = 0;

	while(1)
	{
		system("clear");
		printf("----------------------\n");
		printf("--------1. 插入-------\n");
		printf("--------2. 查询-------\n");
		printf("--------3. 退出-------\n");
		printf("----------------------\n");
		printf("请输入>>>>");
		choose = getchar();
		while(getchar() != 10);

		switch(choose)
		{
		case '1':
			do_insert(db);
			break;
		case '2':
			do_select(db);
			break;
		case '3':
			goto END;
			break;
		default:
			printf("输入错误,请重新输入\n");
		}

		printf("输入任意字符清屏>>>");
		while(getchar() != 10);
	}


END:

	//关闭数据库
	if(sqlite3_close(db) != SQLITE_OK)
	{
		LOG("sqlite3_close error");
		return -1;
	}
	printf("sqlite3_close success\n");


	return 0;
}


int do_insert(sqlite3* db)

{
	FILE* fp_r;
	char word[64] = "";
	char mean[64] = "";
	char buf[128] = "";
	int count = 0;

	if((fp_r = fopen("./dict.txt", "r")) == NULL)
	{
		LOG("open error");
		return -1;
	}


	printf("词典正在录入。。。\n");
	while(1)
	{
		bzero(buf, sizeof(buf));
		bzero(mean, sizeof(mean));
		bzero(word, sizeof(word));

		if(7987 == count++)
			break;
		if(fgets(buf, sizeof(buf), fp_r) == NULL)
		{
			LOG("fgets error");
			return -1;
		}
		buf[strlen(buf)-1] = 0;

		int flag = 0;
		for(int i = 0; i < strlen(buf); i++)
		{
			if(buf[i] == ' ' && 0 == flag)
			{
				strncpy(word, buf, i+1);
				word[strlen(word)-1] = 0;
				flag = 1;
			}

			if(buf[i] != ' ' && 1 == flag)
			{
				strcpy(mean, &buf[i]);
				break;
			}

		}

		char sql[128] = "";
		sprintf(sql, "insert into dict values(\"%s\", \"%s\")", word, mean);
	//	printf("sql = %s \n", sql);
		char* errmsg = NULL;
		if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
		{
			fprintf(stderr, "sqlite3_exec:%s __%d__\n", errmsg, __LINE__);
			return -1;
		}


	}

	printf("词典录入完成\n");

	return 0;
}


int callBack(void *arg, int column, char **column_text, char** column_name)
{
	//打印表头
	if(0 == *(int*)arg)
	{
		for(int i = 0; i < column; i++)
		{
			printf("%s\t", column_name[i]);
		}
		puts("");
		//设置flag只打印一次
		*(int*)arg = 1;
	}

	//打印查询结果
	for(int i = 0; i < column; i++)
	{
		printf("%s\t", column_text[i]);
	}
	puts("");

	//返回值为0表示查询成功
	//若非0返回值会导致sqlite3_exec函数调用失败
	return 0;
}


int do_select(sqlite3* db)
{
	char sql[128] = "select * from dict";
	char** pres = NULL;
	int row, column;
	char* errmsg = NULL;

	if(sqlite3_get_table(db, sql, &pres, &row, &column, &errmsg) != SQLITE_OK)
	{
		fprintf(stderr, "sqlite3_get_table:%s __%d__\n", errmsg, __LINE__);
		return -1;
	}

	printf("select stu success\n");

	//结果中包含表头的那一行,所以要+1
	for(int i = 0; i < (row+1) * column; i++)
	{
		printf("%s\t", pres[i]);
		if((i+1)%column == 0)
			puts("");
	}

	//释放
	sqlite3_free_table(pres);

	return 0;
}
相关推荐
围炉聊科技1 小时前
长期免费的数据库方案——零成本基建系列
数据库
AC赳赳老秦1 小时前
公开图片 OCR 数据提取:OpenClaw 合规采集公开信息图,识别文字与表格并转化为结构化数据
java·大数据·前端·数据库·python·php·openclaw
梁辰兴3 小时前
软件工程:数据结构设计
数据库·软件工程·设计原则·设计方法·梁辰兴·数据库结构设计·数据库结构类型
AC赳赳老秦3 小时前
风控岗应用:OpenClaw 采集公开司法与经营异常数据,自动生成企业风险评估报告
大数据·c语言·数据库·人工智能·python·php·openclaw
灯澜忆梦4 小时前
【MySQL18】进阶篇 | MySQL管理
数据库·mysql
zcmodeltech4 小时前
智能制造教学实训沙盘模型多系统协同控制系统设计:基于STM32与Modbus RTU的工业机器人-智慧工厂-数字孪生全场景联动方案
数据库·stm32·嵌入式硬件·机器人·制造·多分类
AI多Agent协作实战派5 小时前
AI多Agent协作系统实战(四十):AI说“没有错误“,系统判了“测试失败“——一个正则的误判
数据库·人工智能
隔窗听雨眠6 小时前
OceanBase接入DeepSeek:数据库与AI的深度融合如何改写企业数据规则
数据库·人工智能·oceanbase
playboy1346 小时前
工作室 NAS 存储故障-办公室所有数据无法访问怎么办
数据库
byxdaz7 小时前
jeston平台交叉编译Poppler库与预览pdf文件
数据库