Specifying the bucket name in a N1QL query via the C API

I have a server on my local machine with a single bucket named testbucket. I have a connection via the C API that I can put and get documents from. I am now trying to run N1QL queries but am not understanding the error I am getting and would appreciate some help please.

The query I am attempting to run is {“statement”:"SELECT count(*) FROM testbucket"}
The error message from the callback is “Cannot find dataset testbucket in dataverse Default nor an alias with name testbucket”, the callback receiving code LCB_HTTP_ERROR.

The code to run the query is as follows

    lcb_CMDN1QL cmd;
    memset(&cmd, 0, sizeof(lcb_CMDN1QL));

    cmd.cmdflags = LCB_CMDN1QL_F_ANALYTICSQUERY;
    cmd.callback = &static_callback;
    cmd.query = <char * of query>
    cmd.nquery = <length of query>
    lcb_n1ql_query(connection, this, &cmd); // returns LCB_SUCCESS

The connection was setup as follows

connection_string_ = "couchbase://127.0.0.1/testbucket";
struct lcb_create_st options;
memset(&options, 0, sizeof options);
options.version = 3;
options.v.v3.connstr = connection_string_
options.v.v3.username = <data>;
options.v.v3.passwd = <data>;
lcb_create(&connection_, &options);
lcb_connect(connection_);
lcb_wait(connection_);
lcb_get_bootstrap_status(connection_);

The commands to set and retrieve documents appear to use the bucket as specified in the connection string, while that of the query call appears not to. I tried removing the “FROM ‘testbucket’” on the guess that this might be implied by the connection, but that also failed.

When I use the web interface the query string SELECT count(*) FROM testbucket works, so my assumption is that there must be something wrong with the way I am passing this to the API.

Hi @towler

Have you created that dataverse previously?

Check analytics tutorial here: Tutorial | Couchbase Docs

No I hadn’t understood this was necessary as my queries worked in the web interface. I will go and do some reading. Thanks for the quick reply.

I am getting the impression the fundamental knowledge I am missing is what the main difference is between querying and analytics, and why I need both. If I want to do text and value searching of documents which do I need? They both appear to have a similar query language, allow field selection and have indices but there clearly must be some essential difference. I have probably skipped over some primer somewhere…

You can find more about relation between Query and Analytics here: https://docs.couchbase.com/server/6.0/analytics/6_n1ql.html