const char* stmt = qs.str().c_str();
That extracts a temporary string from qs
, takes a pointer to its content, then destroys the temporary leaving the pointer dangling. Using the pointer after this will give undefined behaviour.
To fix it, you could either assign the result of str()
to a variable, so that it's no longer temporary, or use this expression as the argument to sqlite3_exec
, so that the temporary survives until after that function call. (In the second case, you'd have to remove the first assertion; but that assertion is rather pointless anyway).