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;
}
相关推荐
DemonAvenger1 小时前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥12 小时前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud16 小时前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术20 小时前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug1 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom1 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
麦兜*1 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
Slaughter信仰1 天前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第十章知识点问答(10题)
java·jvm·数据库
麦兜*1 天前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring