C SDK problem with populating N1QL query with multiple params with lcb_n1p_namedparamz(...)

Hey there,
Im currently working on implementing the Couchbase C SDK (libcouchbase) to realize a direct connection from a QML Application to Couchbase over N1QL queries. The problem Im facing right now is that I have a function that generates me queries with placeholder values like ($par1, $par2,…, $parN) and I want to fill the placeholders with the corresponding values (defined as a Map) that are passed into the actual function that sends the query to the server. So I wrote some code that is perfectly working for numeric values but when there is a string that is passed the provided function lcb_n1p_namedparamz() seems to not do its job the right way. For example if i have lets say param1 : “24” and param : “typekey” with placeholders $par1 and $par2 only param1 in this case 24 is merged into the query. So my question now is if i have to change some config flags or anything like that so that lcb_n1p_namedparamz() works with non numeric values or do I completely use the wrong function for parsing multiple parameters with multiple types into my query? I will attach some pictures of my code so that you can have a better understanding of what I precisely mean. You will see i have values -13 and “objekt” but in the command structure of the SDK within debugger which is called cmd there I only have $par1 : -13 set up.


Ok It should have definitely something to do with strings because here I have an example where only 1 param is passed (value: “assets” placeholder: “$par1”) and when you look at cmd.query in the debugging window below the value isnt passed into the query but the query itself definitely has a placeholder “$par1” inside so I dont get it why lcb_n1p_namedparamz() isnt exchanging it.Maybe the content-type of the cmd structure has to be changed from “application/json” do something different? I already tried to pass real c-strings with (char*) cast and std::string::c_str() but it makes no difference :frowning:

It must be something wrong with your code sample. I tried to reproduce your problem, but everything works well. The snippet below outputs correctly

{"$par1":"John","$par2":42,"statement":"SELECT * FROM default WHERE name = $par1 AND age > $par2"}
lcb_N1QLPARAMS *builder = lcb_n1p_new();

std::string query("SELECT * FROM default WHERE name = $par1 AND age > $par2");
lcb_n1p_setquery(builder, query.data(), query.size(), LCB_N1P_QUERY_STATEMENT);

std::string name1("$par1");
std::string value1("\"John\"");
lcb_n1p_namedparam(builder, name1.data(), name1.size(), value1.data(), value1.size());

std::string name2("$par2");
std::string value2("42");
lcb_n1p_namedparam(builder, name2.data(), name2.size(), value2.data(), value2.size());

lcb_CMDN1QL cmd;
lcb_n1p_mkcmd(builder, &cmd);

std::cout << cmd.query << std::endl;


Hi avsej,
first of all thank you for the quick answer… Seems like I have to take another deep look into my code maybe there is an issue with qt at some point or something like that. Its so frustrating im almost done with my current project the thing is that most generated queries dont work if the params arent correct :smiley: im new to couchbase but it was really fun to experiment with n1ql and your c sdk! keep up the good work!

1 Like

Hi again,
ive tested some stuff the last 1-2h but nothing solved the problem so if you have any other ideas why this could happen pls let me know :slight_smile:

Do you have standalone example that demonstrates your issue? You can paste code here.

Oh sorry, I totally forgot about answering or closing this thread. I couldnt really figure out the problem so I wrote a workaround function that replaces the placeholders with the parameters powered by QString from Qt. This works quite well so I didnt need to search for the problem any longer. When I rework this thing in the future and maybe find the problem ill let you know what it was if anyone should be facing a similar problem.

Seems like I got it finally… :smiley: The problem was that in my query string generation function the param names were already escaped with ( “”) like “$par1” and so on and therefore lcb_n1p_namedparam returned “LCB_EINVAL” all the time. So what I changed was to remove the signs within the generation function and finally escape the parameter values right before they were parsed into lcb_n1p_namedparam(). Now everything works flawless and my parameterized queries do their job as intended. So if anyone is having similar issues dont forget to escape your parameter values before parsing into lcb_n1p_namedparam() :slight_smile: