使用DataStaxC/c++驱动程序从cassandra数据库获取值

myss37ts  于 2021-06-13  发布在  Cassandra
关注(0)|答案(0)|浏览(378)

我正在尝试使用DataStaxC/c++驱动程序从cassandra数据库检索值。数据库是一个键(主键)-值对。这样做的准则是:

string temp = "SELECT * FROM "+ keyspace+"."+tablename+" WHERE id = ?";
const char* query = (char*)malloc(sizeof(char)*temp.size());
query = (const char*)temp.c_str();

CassStatement* statement = cass_statement_new(query, 1);
cass_statement_bind_string(statement, 0, key);
CassFuture* future = cass_session_execute(session, statement);
cass_future_wait(future);
CassError rc = CASS_OK;
rc = cass_future_error_code(future);
if (rc != CASS_OK) 
{
    print_error(future);
}

const CassResult* result = cass_future_get_result(future);
const CassRow* row = cass_result_first_row(result);
const CassValue* column1 = cass_row_get_column(row, 1);
const char* string_value;
size_t string_value_length;
const CassValue* column3 = cass_row_get_column(row, 2);
cass_value_get_string(column1, &string_value, &string_value_length);

但是它返回损坏的值。例如,如果期望的键是:
vxjvbsoaifzyxnjcberrnmisyjhovengbpyqrixqgwdrygxrxkfhicainhwilkqmbpeszdigznxtzqsjwatycbmjawwmninepfluclltxmkpvgrrgtuseurageltkcapwpbcromqawixezqkvlfbhwcocpjmmbgegvsuluqtuuvkesvjtdhvtjmexfqbvufdpaxcwqjtbplyzedicwsodpwtqrpyuearhwgfnpaqelofrsotqikt
它返回:
vxjvbsoaifzyxnjcberrnmisyjhovengbpyqrixqgwdrygxrxkfhicainhwilkqmbpeszdigznxtzqsjwatycbmjawwmninepfluclltxmkpvgrrgtuseurageltkcapwpbcromqawixezqkvlfbhwcocpjmmbgegvsuluqtuuvkesvjtdhvtjmexfqbvufdpaxcwqjtbplyzedicwsodpwtqrprpwwqueloffp
要插入的代码是:

CassError rc = CASS_OK;
CassStatement* statement = NULL;
CassFuture* future =NULL;

string temp="INSERT INTO "+ keyspace+"."+tablename+" (id,value) VALUES(?,?);";
const char* query = (char*)malloc(sizeof(char)*temp.size());
query = (const char*)temp.c_str();

statement = cass_statement_new(query, 2);
cass_statement_bind_string(statement, 0, key);
cass_statement_bind_string(statement, 1, value);
future =  cass_session_execute(session, statement);

cass_future_wait(future);
rc = cass_future_error_code(future);
if (rc != CASS_OK) 
{
    print_error(future);
}
cass_future_free(future);
cass_statement_free(statement);

当我用cqlsh检查时,数据库包含正确的值。有人能指出如何解决这个问题吗?短暂性脑缺血发作

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题