Skip to content

Instantly share code, notes, and snippets.

@mnunberg
Created February 20, 2015 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnunberg/bb753f48c07d740fcb35 to your computer and use it in GitHub Desktop.
Save mnunberg/bb753f48c07d740fcb35 to your computer and use it in GitHub Desktop.
n1ql/lcb2.4.7
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <libcouchbase/couchbase.h>
#include <libcouchbase/n1ql.h>
#define CONNSTR "couchbase://localhost:12000/beer-sample"
static void rowCallback(lcb_t instance, int cbtype, const lcb_RESPN1QL *resp) {
if (! (resp->rflags & LCB_RESP_F_FINAL)) {
printf("Row: %.*s\n", (int)resp->nrow, resp->row);
} else {
printf("Got metadata: %.*s\n", (int)resp->nrow, resp->row);
}
}
int main(int argc, char **argv)
{
struct lcb_create_st crst = {
.version = 3,
.v.v3.connstr = CONNSTR
};
lcb_t instance;
lcb_error_t rc;
lcb_create(&instance, &crst);
lcb_connect(instance);
lcb_wait(instance);
assert(lcb_get_bootstrap_status(instance) == LCB_SUCCESS);
// Initialize the queries:
lcb_CMDN1QL cmd = { 0 };
// Allocate the parameter object
lcb_N1QLPARAMS *nparams = lcb_n1p_new();
rc = lcb_n1p_setstmtz(nparams, "SELECT fname || \" \" || lname, age FROM default WHERE age > $age LIMIT 5");
assert(rc == LCB_SUCCESS);
// Set the value for '$age'
rc = lcb_n1p_namedparamz(nparams, "$age", "27");
assert(rc == LCB_SUCCESS);
// Now, fill the command structure
lcb_n1p_mkcmd(nparams, &cmd);
cmd.callback = rowCallback;
// For now, we will use the standalone N1QL DP4 package; but you can
// also use the embedded n1ql obtained from master server builds
cmd.host = "localhost:8093";
rc = lcb_n1ql_query(instance, NULL, &cmd);
assert(rc == LCB_SUCCESS);
// We can release the params object now..
lcb_n1p_free(nparams);
lcb_wait(instance);
lcb_destroy(instance);
return 0;
}
@GauthamBanasandra
Copy link

GauthamBanasandra commented Feb 3, 2017

Hi,
I got the following error upon running this.
error: non-aggregate type 'struct lcb_create_st' cannot be initialized with an initializer list
struct lcb_create_st crst = {
^ ~
Please change the struct initialisation of "crst" as follows -
lcb_create_st crst;
memset(&crst, 0, sizeof(crst));
crst.version = 3;
crst.v.v3.connstr = CONNSTR;

You can find the full code here - https://gist.github.com/GauthamBanasandra/556bb8288217767492746d190f56f546

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment