数据库查询操作(sqlite_get_table)

#include <stdio.h>
#include <sqlite3.h>

// 直接查询而不需要回调
// 数据库查询操作,无回调
int main()
{
	sqlite3 *db; //声明sqlite关键结构指针	
	
	// 1、新建数据文件
	int ret = sqlite3_open("student.db", &db);
	
	if( ret != SQLITE_OK )
	{
		printf ("数据库打开失败\n");
		
		return -1;
	}
	
	// 第一个参数:句柄
	
	// 第二个参数:sq1语句
	const char *sq1 = "select * from student";
	
	// 第三个参数:结果写入该指针指向的char***
	char **value = NULL;
	
	// 第四个参数:行
	int row;
	
	// 第五个参数:列
	int column;
	
	// 第六个参数:错误信息
	char *errmsg;
	
	// 2、在数据库中新建表
	// int sqlite3_get_table(sqlite3*, const char *sql, char ***resultp, int *nrow, int *ncolumn, char **errmsg );
	ret = sqlite3_get_table(db, sq1, &value, &row, &column, &errmsg );
	
	if( ret != SQLITE_OK )
	{
		printf ("exec1失败:%s\n", errmsg);
		sqlite3_free(errmsg);
		
		return -1;
	}
	
	int i;
	
	for (i = 0; i < row * column; i++)
	{
		if (row != 0 && i%column == 0)
		{
			printf ("\n");
		}
		
		printf ("%-8s", value[i]);
	}
	
	printf ("\n");
	
	sqlite3_free_table(value);
	sqlite3_close(db);
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值