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;
}
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);
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");
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)
{
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");
}
}
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());
}
}
补充说明:
- 方括号 [ ]
方括号 ( [ ] ) 表示里面的元素(参数、值或信息)是可选的。 您可以选择一个或多个条目,也可以不选。 不要将方括号本身也输入到命令行中。 - 尖括号 < >
尖括号 ( < > ) 表示里面的元素(参数、值或信息)是必需的。 您需要用相应的信息来替换尖括号里面的文本。 不要将尖括号本身也输入到命令行中。 - [ ,…n ] 表示前面的项可重复 n 次。每一项由逗号分隔。
- [ …n ] 表示前面的项可重复 n 次。每一项由空格分隔。
- | (竖线) 分隔括号或大括号内的语法项目。只能选择一个项目。