I have the following code for a simple query of insertion into Cassandra. I am trying to use prepared statement here as the regular statement is not able to insert the timestamp in timestamp column of table time_demo
.
CassFuture* connect_future = NULL;
CassCluster* cluster = cass_cluster_new();
CassSession* session = cass_session_new();
char* hosts = "127.0.0.1"; time_t rt= time(NULL);
struct tm * timeinfo; timeinfo = localtime ( &rt );
char lt[20];
strftime(lt, sizeof(lt), "%Y-%m-%d %T", timeinfo);
/* Add contact points */
cass_cluster_set_contact_points(cluster, hosts); /* Provide the cluster object as configuration to connect the session with a specified keyspace*/
connect_future = cass_session_connect_keyspace(session, cluster,"test_keyspace"); if(cass_future_error_code(connect_future)== CASS_OK) { CassFuture* prepare_future = cass_session_prepare(session, "INSERT INTO time_demo(id,time) VALUES(now(),?);"); if (cass_future_error_code(prepare_future) == CASS_OK) { /* Get the prepared object from the future */ const CassPrepared* prepared = cass_future_get_prepared(prepare_future); /* The future can be freed immediately after getting the prepared object */ cass_future_free(prepare_future); /* The prepared object can now be used to create statements that can be executed */ CassStatement* statement = cass_prepared_bind(prepared); /* Bind variables by name this time (this can only be done with prepared statements)*/ cass_statement_bind_string_by_name(statement, "time", lt); CassFuture* result_future = cass_session_execute(session, statement); cass_prepared_free(prepared); CassError rc = cass_future_error_code(result_future); printf("Query result %s ",cass_error_desc(rc)); } cass_cluster_free(cluster); cass_session_free(session); } }
The above code is not able to insert timestamp into time_demo table. It is only inserting NULL instead. Can someone please tell the correct way of inserting the timestamp?
Also, is it only possible to insert timestamp using prepared statement but not regular statement because I have tried inserting timestamp
with regular statement but in that case nothing was getting inserted. The table was completely blank.
I am using C driver for Cassandra version 2.13.