sqlite的文件导入操作

#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;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值