第9章 第12讲 FarelDB 关系二维表操作命令(table) 之 Batch Select

FarelDB 关系数据库命令 数据库操作类(database) 之 高性能批量查询 Select

高性能批量查询用于需要快速的批量查询大量数据的应用场景,该命令仅能被API接口调用。


API 说明

函数原型

FarelDbRes* batchSelect(std::string dbName, std::string tableName, std::vector<std::vector<std::string>>& keyLists, std::vector<std::string>& selectFieldNames)

输入参数说明

输入参数类型描述可选
dbName字符串操作表所属数据名称
tableName字符串操作表名称
keyLists二维数组向量需要查询的行的行主键值
selectFieldNames一维度数组向量需要查询的列的列名

返回参数说明

调用成功返回

返回参数含义
FarelDbRes->errorInfos.size()0调用成功,无错误信息

调用异常返回

返回参数含义
FarelDbRes->errorInfos.size()大于0调用失败,errorInfos对象数组包含1个或多个错误信息
FarelDbRes->errorInfos[0]->resCode大于0的数值调用失败,返回错误代码
FarelDbRes->errorInfos[0]->resInfo非空字符串调用失败,返回错误信息字符串

返回数据获取方法参考样例代码

样例代码

#include <iostream>
#include <functional>
#include <memory>
#include "fareldb_connection.h"
using namespace std;
using namespace fareldb_connection;

int main(int argc,char *argv[])
{
	//创建连接
	FarelDbConnection  conn;

	//连接
	std::tuple<int, string> resConn = conn.connect("127.0.0.1", 6800, "root", "root");
		//连接失败
	if (0 != get<0>(resConn))
	{
		printf("连接失败,错误代码:%d : 错误信息:%s\n", get<0>(resConn), get<1>(resConn).c_str());
		return;
	}

	//连接成功,可以开始进行后续操作

	//第1步.传入需要查询的行的行主键值
	vector<vector<string> > keyLists;
	vector<string> IDKeyValue;
	IDKeyValue.clear();
	IDKeyValue.emplace_back("1");  //所有主键以字符串传入
	keyLists.emplace_back(IDKeyValue);

	IDKeyValue.clear();
	IDKeyValue.emplace_back("3");  //所有主键以字符串传入
	keyLists.emplace_back(IDKeyValue);

	IDKeyValue.clear();
	IDKeyValue.emplace_back("4");  //所有主键以字符串传入
	keyLists.emplace_back(IDKeyValue);

	IDKeyValue.clear();
	IDKeyValue.emplace_back("5");  //所有主键以字符串传入
	keyLists.emplace_back(IDKeyValue);

	//本例中student只有一个主键,即ID,如果查询行的主键存在多个(联合主键),则按照下面例子以此插入keyLists即可。 
	//vector<string> OthersKeyValue;
	//OthersKeyValue.clear();
	//OthersKeyValue.emplace_back("a");  //所有主键以字符串传入
	//keyLists.emplace_back(OthersKeyValue);

	//OthersKeyValue.clear();
	//OthersKeyValue.emplace_back("b");  //所有主键以字符串传入
	//keyLists.emplace_back(OthersKeyValue);

	//OthersKeyValue.clear();
	//OthersKeyValue.emplace_back("c");  //所有主键以字符串传入
	//keyLists.emplace_back(OthersKeyValue);

	//OthersKeyValue.clear();
	//OthersKeyValue.emplace_back("d");  //所有主键以字符串传入
	//keyLists.emplace_back(OthersKeyValue);

	 //第2步. 传入查询列的列信息
	 std::vector<std::string> selectFieldNames;
	 selectFieldNames.emplace_back("ID");
	 selectFieldNames.emplace_back("Name");
	 selectFieldNames.emplace_back("Scholarship");
	 selectFieldNames.emplace_back("Age");
	 selectFieldNames.emplace_back("Score");
 
	  
	 //第3步 调用批量更新接口
	 std::shared_ptr<FarelDbRes> res(conn.batchSelect("studentDB", "student", keyLists, selectFieldNames));

	 //判断执行命令返回是否失败
	 if (res->errorInfos.size() > 0)
	 {
		 printf("batchUpdate 调用失败,错误代码:%d  ; 错误信息: %s \n\n", res->errorInfos[0]->resCode, res->errorInfos[0]->resInfo.c_str());
		 return;
	 }

	//打印返回数据表的信息
	if (res->fields.size() > 0)  //返回数据表的列信息(表头信息数据字典)在FarelDbRes->fields数组中存储
	{
		for (size_t i = 0; i < res->fields.size(); i++)
		{
			FarelDbField* field = res->fields[i];
			printf("%s\t", field->name.c_str());
			if (res->fields.size() - 1 == i)
			{
				printf("\n");
			}
		}
		//返回数据表的行信息(记录数据)在FarelDbRes->rows 的数组中存储
		for (size_t i = 0; i < res->rows.size(); i++)
		{
			FarelDbRow& row = res->rows[i];
			for (size_t j = 0; j < row.size(); j++)
			{
				if (nullptr != row[j]->data)
				{
					//根据列类型获取,获取表格对应单元格的字符串格式数据
					string strVal = conn.getStrValFromColVal(res->fields[j], row[j]->data);
					strVal = strVal.length() == 0 ? "\"\"" : strVal;
					printf("%s    ", strVal.c_str());
				}
				else
				{
					printf("NULL\t");
				}
			}
			if (i < res->rows.size())
			{
				printf("\n");
			}
		}
		printf("[%llu rows]\n", res->rows.size());
	}
}

补充说明:

  1. 方括号 [ ]
    方括号 ( [ ] ) 表示里面的元素(参数、值或信息)是可选的。 您可以选择一个或多个条目,也可以不选。 不要将方括号本身也输入到命令行中。
  2. 尖括号 < >
    尖括号 ( < > ) 表示里面的元素(参数、值或信息)是必需的。 您需要用相应的信息来替换尖括号里面的文本。 不要将尖括号本身也输入到命令行中。
  3. [ ,…n ] 表示前面的项可重复 n 次。每一项由逗号分隔。
  4. [ …n ] 表示前面的项可重复 n 次。每一项由空格分隔。
  5. | (竖线) 分隔括号或大括号内的语法项目。只能选择一个项目。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值