如何在MYSQL xdevapi 8.0连接器c++中通过字段名称获取值
对于连接器 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 thegetColumnLabel()
- something like
indexOf
? But I findresult.getColumns()
does not support this method since it's adeque
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前无法使用名称获取列值。
正如您所建议的,一种方法是带有名称/索引对的映射。
像这样的东西会起作用:
然后,使用它:
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:
And then, using it: