如何在MYSQL xdevapi 8.0连接器c++中通过字段名称获取值

发布于 2025-01-09 14:01:50 字数 1042 浏览 4 评论 0原文

对于连接器 c++ 1.1,在此 示例,通过指定列名(或别名)来获取值非常容易。
但是当我升级到xdevapi 8.0版本后,我发现这个功能不再支持了。

#include <mysqlx/xdevapi.h>

using namespace std;
using namespace mysqlx;

Session sess(<server_url>);
auto result = sess.sql("SELECT * FROM student").execute();
Row row = result.fetchOne();
cout << row[0] << endl;         // <- ok
cout << row["ID"] << endl;      // <- can compile but garbage output
cout << row.get("ID") << endl;  // <- cannot compile

我知道可以从 result.getColumn(n).getColumnLabel() 检索列名称,但在我看来,它毫无用处。一个“场”-> “索引”映射确实可以帮助开发人员。

我是 C++ 新手,所以下面的句子可能太天真了。我猜测可能的方法如下:

  • 构造一个STL映射来记录映射,
  • 通过result.getColumns()迭代,然后检查getColumnLabel()
  • 类似indexOf的东西?但我发现 result.getColumns() 不支持此方法,因为它是一个 deque

For connector c++ 1.1, in this example, it's quite easy to get values by specifying the column name (or alias name).
But when I upgraded to version 8.0 xdevapi, I found this feature is no longer supported.

#include <mysqlx/xdevapi.h>

using namespace std;
using namespace mysqlx;

Session sess(<server_url>);
auto result = sess.sql("SELECT * FROM student").execute();
Row row = result.fetchOne();
cout << row[0] << endl;         // <- ok
cout << row["ID"] << endl;      // <- can compile but garbage output
cout << row.get("ID") << endl;  // <- cannot compile

I know the column names can be retrieved from result.getColumn(n).getColumnLabel(), but IMO it's useless. A "field" -> "index" mapping can really help the developers.

I'm new to C++, so the following sentenses maybe too naive. Here's the possible ways I guess:

  • construct a STL map to record the mapping
  • iterate through the result.getColumns(), then check the getColumnLabel()
  • something like indexOf? But I find result.getColumns() does not support this method since it's a deque

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2025-01-16 14:01:50

目前无法使用名称获取列值。

正如您所建议的,一种方法是带有名称/索引对的映射。

像这样的东西会起作用:

SqlResult sql_res = sess.sql("select * from table").execute();

std::map<std::string, size_t> columns;
auto fill_columns = [&sql_res, &columns]() -> void
{
  size_t idx = 0;
  for (auto &el : sql_res.getColumns())
  {
    columns.emplace(std::make_pair(el.getColumnLabel(), idx++));
  }
};

fill_columns();

然后,使用它:

for(auto row : sql_res)
{
  std::cout << "a:" << row[columns["a"]] << std::endl;
  std::cout << "b:" << row[columns["b"]] << std::endl;
}

There is no way, at the moment, to get the column value using name.

A way is, as you suggest, a map with the name / index pair.

Something like this would work:

SqlResult sql_res = sess.sql("select * from table").execute();

std::map<std::string, size_t> columns;
auto fill_columns = [&sql_res, &columns]() -> void
{
  size_t idx = 0;
  for (auto &el : sql_res.getColumns())
  {
    columns.emplace(std::make_pair(el.getColumnLabel(), idx++));
  }
};

fill_columns();

And then, using it:

for(auto row : sql_res)
{
  std::cout << "a:" << row[columns["a"]] << std::endl;
  std::cout << "b:" << row[columns["b"]] << std::endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文