SOCI / soci

Official repository of the SOCI - The C++ Database Access Library
http://soci.sourceforge.net/
Boost Software License 1.0
1.37k stars 472 forks source link

when inserting into postgresql some characters will turn to dollar sign with followup number #1044

Closed atari83 closed 1 year ago

atari83 commented 1 year ago

Hello ! I'm using soci 4.0.1 with Postgresql 14.0 on my FreeBSD 13.0 (amd64) machine. All is good, except I noticed when I batch/bulk insert some json (as text) into my table, the inserted data is somehow modified ! Honestly, I'm not sure it is related to soci, but I'm totally confused and will be appreciated for any help.

So, for insert I use this snippet code:

try {
soci::session s ( conn_pool );
s << "insert into mytable (...) values " << var << ";" ;
}
catch ...

The var is a string variable that contains multiple values that need to be inserted. There is no errors and records are getting inserted into the table, however If my Original data is: {"key":"value"}, {"key2":"value2"} Then when i query from database, I'll have: {"key"$1"value"}, {"key2"$2"value2"}

The colon signs will turn to dollar sign with follow up numbers.

It's worth to mention I don't encounter this issue, when i use postgresql console with same sql command.

vadz commented 1 year ago

Please use soci::use() instead of string concatenation for your query parameters. Doing the latter is a really bad idea for too many reasons (search for "SQL injection" for a start) and also misinterprets :foo as parameter name.

atari83 commented 1 year ago

Thank youuuu !! You're absolutely life-saver ! Sorry for my mistake, totally forgot about soci::use ^_^!