-
This page in the docs talks about prepared statements, which are, as I understand it, a way to run a Cypher statement that is a query we pass arguments to. Well, I need that function in my C++ application, but I'm having trouble figuring out how to implement it. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @b3n-h4il, You can follow how it is done in our C-API binding: https://github.com/kuzudb/kuzu/blob/master/src/c_api/prepared_statement.cpp |
Beta Was this translation helpful? Give feedback.
-
Here is a concrete example: #include "kuzu.hpp"
#include <iostream>
using namespace kuzu::main;
using namespace kuzu::common;
int main(int argc, char **argv)
{
auto database = std::make_unique<Database>(":memory:", SystemConfig());
auto connection = std::make_unique<Connection>(database.get());
auto query = "RETURN $1, $2, $3";
std::unordered_map<std::string, std::unique_ptr<Value>> params;
params["1"] = std::make_unique<Value>(int64_t(1));
params["2"] = std::make_unique<Value>(float(3.14159));
params["3"] = std::make_unique<Value>(std::string("Hello, World!"));
auto prepared_statement = connection->prepare(query);
auto result = connection->executeWithParams(prepared_statement.get(), std::move(params));
if (result->isSuccess()) {
std::cout << "Query executed successfully!" << std::endl;
} else {
std::cout << "Query execution failed: " << result->getErrorMessage() << std::endl;
}
auto result_string = result->toString();
std::cout << "Result: " << result_string << std::endl;
return 0;
} This should print:
|
Beta Was this translation helpful? Give feedback.
Here is a concrete example: