Take a closer look at this line:
const char* stmt = qs.str().c_str();
qs.str()
provides you a temporary std::string
object from which you take a pointer to inner char*
array. Once this line completes its execution, your pointer is no longer valid and something else may (and probably is) stored there.
Try doing this instead:
std::string strstmt(qs.str());
const char* stmt = strstmt.c_str();