通过MySQL-X-Devapi在C++中插入MySQL内置函数的结果?

发布于 2025-02-11 11:12:02 字数 785 浏览 2 评论 0 原文

我正在使用MySQL-X-DEVAPI,需要将行插入表格,并在列中添加服务器的 unix_timestamp()

sql_client_.getSession().getDefaultSchema()
    .getTable("event")
    .insert("title", "time")
    .values("event title", "UNIX_TIMESTAMP()")
    .execute();

此代码为我提供: CDK错误:不正确的整数值'UNIX_TIMESTAMP()'for column'Time'在第1列中

我该如何使用Xdevapi(不是需要SQL String的SQL命令)?

我可以在更新表时使用 mysqlx :: expr(“ unix_timestamp()”) set 函数。同样的情况不适用于 insert ,并且失败了以下错误:

/usr/include/mysql-cppconn-8/mysqlx/devapi/table_crud.h:157:17: error: ‘mysqlx::abi2::r0::internal::Expression::Expression(V&&) [with V = mysqlx::abi2::r0::internal::Expression&]’ is private within this context
  157 |       add_values(get_impl(), rest...);

I am using mysql-x-devapi and need to insert a row to a table and put UNIX_TIMESTAMP() of the server in a column:

sql_client_.getSession().getDefaultSchema()
    .getTable("event")
    .insert("title", "time")
    .values("event title", "UNIX_TIMESTAMP()")
    .execute();

This code gives me:
CDK Error: Incorrect integer value 'UNIX_TIMESTAMP()' for column 'time' at row 1

How can I do this using xdevapi (not sql command that needs sql string)?

I am able to use mysqlx::expr("UNIX_TIMESTAMP()") in set function when updating the table. The same doesn't work with insert and fails with the following error:

/usr/include/mysql-cppconn-8/mysqlx/devapi/table_crud.h:157:17: error: ‘mysqlx::abi2::r0::internal::Expression::Expression(V&&) [with V = mysqlx::abi2::r0::internal::Expression&]’ is private within this context
  157 |       add_values(get_impl(), rest...);

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

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

发布评论

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

评论(2

我做我的改变 2025-02-18 11:12:02

X Devapi用户指南给出了有关是否应该支持的内容的一些提示。在这种情况下,您可以从函数定义文档, values()方法期望a 文字 代码> mysqlx.expr()将起作用。

在您的情况下,查看错误消息,这意味着 time 列可能是某种整数数据类型。给定的“ unix_timestamp()” 是字符串(因为未评估表达式),它不会以任何方式强制强制,并且仅适用于其他列数据类型,例如 varchar varchar < /代码>。

从理论上讲,据我所知,没有什么能真正阻止API允许计算出的表达式。 X协议可容纳,但我不完全确定X插件本身是相同的情况。我建议您使用public mysql )。

免责声明:我是node.js mysql x devapi连接器的首席开发人员

The X DevAPI user guide gives some hints about what should be supported or not. In that case, as you can see from the function definition documentation, the values() method expects a Literal, so, not even mysqlx.expr() will work.

In your case, looking at the error message, it means that the time column probably is some kind of INTEGER data type. Given "UNIX_TIMESTAMP()" is a string (because the expression is not being evaluated), it is not being coerced in any way and would only work for other column data types such as VARCHAR.

In theory, from what I can tell, there is nothing that really prevents the API from allowing computed expressions. The X Protocol accommodates that but I'm not entirely sure it is the same case for the X Plugin itself. I would suggest you request the feature using the public MySQL bug tracker and the corresponding category (maybe MySQL Connectors: Document Store: DevAPI).

Disclaimer: I'm the lead developer of the MySQL X DevAPI Connector for Node.js

日记撕了你也走了 2025-02-18 11:12:02

我使用 mySQLX :: expr(“ unix_timestamp()”)

sql_client_.getSession().getDefaultSchema()
    .getTable("event")
    .insert("title", "time")
    .values("event title", mysqlx::expr("UNIX_TIMESTAMP()"))
    .execute();

然后通过触摸修复编译错误:

/usr/include/mysql/mysql-cppconn-8/mysqlx/mysqlx/devapi/devapi/table_crud.h 替换:

  template<typename... Types>
  TableInsert& values(Types... rest)
  {
    try {
      add_values(get_impl(), rest...);
      return *this;
    }
    CATCH_AND_WRAP
  }

用:

  template<typename... Types>
  TableInsert& values(Types&&... rest)
  {
    try {
      add_values(get_impl(), std::forward<Types>(rest)...);
      return *this;
    }
    CATCH_AND_WRAP
  }

/usr/include/mysql-cppconn-8/mysqlx/devapi/detail/detail/crud.h 替换

  template <typename... T>
  static void add_values(Impl *impl, T... args)
  {
    Add_value::Impl row{ {}, 0 };
    Args_processor<Add_value>::process_args(&row, args...);
    Add_row::process_one(impl, row.first);
  }

  template <typename... T>
  static void add_values(Impl *impl, T&&... args)
  {
    Add_value::Impl row{ {}, 0 };
    Args_processor<Add_value>::process_args(&row, std::forward<T>(args)...);
    Add_row::process_one(impl, row.first);
  }

I used mysqlx::expr("UNIX_TIMESTAMP()"):

sql_client_.getSession().getDefaultSchema()
    .getTable("event")
    .insert("title", "time")
    .values("event title", mysqlx::expr("UNIX_TIMESTAMP()"))
    .execute();

then fixed the compile error by touching:

/usr/include/mysql-cppconn-8/mysqlx/devapi/table_crud.h replaced:

  template<typename... Types>
  TableInsert& values(Types... rest)
  {
    try {
      add_values(get_impl(), rest...);
      return *this;
    }
    CATCH_AND_WRAP
  }

with:

  template<typename... Types>
  TableInsert& values(Types&&... rest)
  {
    try {
      add_values(get_impl(), std::forward<Types>(rest)...);
      return *this;
    }
    CATCH_AND_WRAP
  }

/usr/include/mysql-cppconn-8/mysqlx/devapi/detail/crud.h replaced:

  template <typename... T>
  static void add_values(Impl *impl, T... args)
  {
    Add_value::Impl row{ {}, 0 };
    Args_processor<Add_value>::process_args(&row, args...);
    Add_row::process_one(impl, row.first);
  }

with:

  template <typename... T>
  static void add_values(Impl *impl, T&&... args)
  {
    Add_value::Impl row{ {}, 0 };
    Args_processor<Add_value>::process_args(&row, std::forward<T>(args)...);
    Add_row::process_one(impl, row.first);
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文