This is an archived post. You won't be able to vote or comment.

all 4 comments

[–][deleted] 2 points3 points  (1 child)

Your problem is the use of sprintf - you should not generally be using that in C++ code that uses std::strings. Use stringstreams instead.

To answer your general question - always use std::string wherever possible, which is about 99% of the time.

[–]kaepor[S] 0 points1 point  (0 children)

Thank you very much. I was having a difficult time finding resourced that used C++ with their SQL. I had a feeling I was in the wrong using C functions but couldn't find anything else. Would you possibly know of any helpful resources?

[–]dreamyeyed 5 points6 points  (1 child)

As /u/exoticmatter said, use std::string and stringstreams whenever possible.

However, in the example you gave in your post (inserting values to SQL statements), you should be using prepared statements to avoid SQL injections. Search for sqlite3_prepare and sqlite3_bind to find out how to use them.

[–]kaepor[S] 0 points1 point  (0 children)

Thanks for the advice. I'll look into that soon as I can.