Created
February 20, 2015 16:53
-
-
Save mnunberg/bb753f48c07d740fcb35 to your computer and use it in GitHub Desktop.
n1ql/lcb2.4.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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