EN VI

C++ - Building query dynamically with fmt or std::format?

2024-03-16 09:30:05
How to C++ - Building query dynamically with fmt or std::format

I am trying to build a SQL query whose parameter list is dynamic in a more modern way than with a stringstream.

What I want to obtain is

WHERE param=@p0 OR param=@p1 OR ...

I tried this

std::vector<int> ids(input.size());
std::iota(ids.begin(), ids.end(), 0);

// Results in 0param=@p{} OR 1
std::string clause = fmt::format("{}", fmt::join(ids, "param=@p{} OR ");

// Results in param=@p0 OR 1
std::string clause = fmt::format("param=@p{}", fmt::join(ids, " OR ");

Solution:

Maybe something like this can

std::string clause = fmt::format("param=@p{}", fmt::join(ids, " OR param=@p"));
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login