First off, C++ is a new language to me although I have coded in a few others; so please forgive any ignorance of C++ on my part. I've worked out how to do database connections, and simple queries; however the thing that's throwing me completely is the use of variables in said queries.
I've got the following query that happily inserts data into my DB, but I can't for the life of me work out how to do the same query but inserting a variable instead of static text.
if (SQL_SUCCESS != SQLExecDirect(sqlstatementhandle, (SQLCHAR*)"insert into testtable (name) values ('testname');", SQL_NTS)) {
show_error(SQL_HANDLE_STMT, sqlstatementhandle);
goto FINISHED;
}
I've done quite a lot of reading around but nobody seems to give a clear, or even the same, answer. The only constant theme is that, for obvious reasons, I should be using a prepared statement. However even this method varies wildly in application from person to person.
Using VS2015 and SQL Server 2014.
UPDATE
Thanks for all of the pointers, I've added the below code and it's now happily inserting variables!
char* newvar;
newvar = "preptest";
SQLCHAR newquery[100];
sprintf((char*)newquery, "INSERT INTO testtable (name) VALUES ('%s');", newvar);
if (SQL_SUCCESS != SQLExecDirect(sqlstatementhandle, newquery, SQL_NTS)) {
show_error(SQL_HANDLE_STMT, sqlstatementhandle);
}
If (lol, if; when) you see any other no-nos please feel free to point them out. A quick follow up question, if the variable needs to include an escape character, how do I go about ignoring it? Or do I just use RE to escape the escape char?