sqlite的文件导入操作

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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

你可能感兴趣的:(sqlite,数据库)