In my C++ code I get events about data that has to be inserted in the database.
The events produce different threads and I have a BlockingConcurrentQueue that is used in a producer consumer model. Every thread writes (produces) in the queue something like:
INSERT INTO CHAT_COMMENTS (chat_comment_id, comment) values (3,'This is a comment';
The above string generated as
sprintf(insert_statement, "INSERT INTO CHAT_COMMENTS (chat_comment_id, comment) values (%d,'%s')",e->id,e->comment);
A scheduler runs every a while and executes all of these insert statements in a MySQL database. Now, the problem is that comments might have some MySQL special characters as shown below:
cout << comment; // produces "this_% LIKE 'a comment """\m/'DROP TABLE USERS"
INSERT INTO CHAT_COMMENTS (chat_comment_id, comment) values (3,'this_% LIKE 'a comment """\m/'DROP TABLE USERS');
Is there a way to handle these case?
I know that one can use prepared statements, but I am looking for something different as PreparedStatements are not thread safe and also because I want to execute the queries above in batches.