If I have a variable like:
char *sql;
sql = "insert into Norm1Tab values (?,?,?,?,?,?)";
I would like to replace each ? by values that are stored in other char or char* variables. How I can do that in C++ ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you are using sqlite (sure looks like it), you do not have to do the string replaces yourself. Use
sqlite3_bind_*instead.bind documentation
There are a lot of benefits from using the APIs from your database provider, so you really shouldn’t avoid them if you can help it. You’ll have better type safety, better protection against injection, and way better performance.
Otherwise, I would use
boost::formatfor this.As mentioned by others, you’ll need to clean your params to make sure there aren’t any injection vulnerabilities.
If you don’t, anything with special characters could break it.
It would take some knowledge of the structure to destroy it. As soon as someone saw the error message from the
Joe's House, they would probably know that they could do worse.If you do this consistently, it is only a matter of time before a smart person is going to have your full schema. With sqlite for instance, any injection on a query could get you all the information you needed to quietly modify your records in any way that they wanted.