gpt4 book ai didi

c++ - 如何在 INSERT、Cassandra C++ 驱动程序中将值绑定(bind)到 TTL

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:32:47 24 4
gpt4 key购买 nike

我在 Cassandra Datastax C++ 驱动程序中使用准备好的语句。如何将整数值绑定(bind)到“USING TTL”?准备好的陈述的一部分?

我的声明是这样的

INSERT INTO table (column1, column2, column3)  VALUES (?, ?, ?) USING TTL ?

换句话说,如果我使用位置绑定(bind)到 TTL,它的位置是什么? (在这个例子中,是 4 吗?)如果我使用按列名绑定(bind),它的列名是什么?

看起来这可以在 CQL 中完成,但我找不到任何关于用于执行此操作的 C++ 驱动程序 API 的文档。

最佳答案

在 Cassandra CQL 2.0 中,您可以:

Cassandra 1.2 doesn't allow you to use a bind marker for the TIMESTAMP and TTL properties of update statements, nor for the LIMIT property of SELECT statements. This is now fixed and you can for instance prepare statements like:

SELECT * FROM myTable LIMIT ?;
UPDATE myTable USING TTL ? SET v = 2 WHERE k = 'foo';

查看他们的 blog更多。

编辑:

我找到了 this pdf它告诉你更多:

绑定(bind)参数:

The driver supports two kinds of bound parameters: by marker and by name. Binding parameters The ? marker is used to denote the bind variables in a query string. This is used for both regular and prepared parameterized queries. In addition to adding the bind marker to your query string, your application must also provide the number of bind variables to cass_statement_new() when constructing a new statement. If a query doesn’t require any bind variables then 0 can be used. The cass_statement_bind_*() functions are then used to bind values to the statement’s variables. Bind variables can be bound by the marker index or by name.

按标记索引示例绑定(bind)

CassString query = cass_string_init("SELECT * FROM table1 WHERE column1
= ?");
/* Create a statement with a single parameter */
CassStatement* statement = cass_statement_new(query, 1);
cass_statement_bind_string(statement, 0, cass_string_init("abc"));
/* Execute statement */
cass_statement_free(statement);

按标记名称示例绑定(bind)

Variables can only be bound by name for prepared statements. This limitation exists because query metadata provided by Cassandra is required to map the variable name to the variable’s marker index.

/* Prepare statement */
/* The prepared query allocates the correct number of parameters
automatically */
CassStatement* statement = cass_prepared_bind(prepared);
/* The parameter can now be bound by name */
cass_statement_bind_string_by_name(statement, "column1",
cass_string_init("abc"));
/* Execute statement */
cass_statement_free(statement);

要回答您的问题,您可以使用按索引绑定(bind)(至少可以确定):

CassString query = cass_string_init("INSERT INTO table (column1, column2, column3)  VALUES (?, ?, ?) USING TTL ?");
/* Create a statement with a single parameter */
CassStatement* statement = cass_statement_new(query, 4); // Bind 4 variables.
cass_statement_bind_string(statement, 0, cass_string_init("abc")); // Bind abc to first column.
cass_statement_bind_string(statement, 1, cass_string_init("bcd")); // Bind bcd to second column.
cass_statement_bind_string(statement, 2, cass_string_init("cde")); // Bind cde to third column.
cass_statement_bind_string(statement, 3, cass_string_init(50)); // Bind 50 to TTL.
/* Execute statement */
cass_statement_free(statement);

编辑:

参见 https://docs.datastax.com/en/cql/3.3/cql/cql_using/useExpireExample.html您会看到,在 INSERT 的情况下,我们将 USING TTL 作为查询的最后一部分,如上所示。

关于c++ - 如何在 INSERT、Cassandra C++ 驱动程序中将值绑定(bind)到 TTL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50995145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com