> You may probably already know this but maybe I'll remind you.  The pointer
> returned by c_str() is only valid in the statement where it is used or 
> possibly
> as long as the life of the basic_string<char> it came from.

Good catch, Jeff! I thought it's so obvious that I didn't even
consider that as a potential problem. So this code can easily write
trash into database:

     idx = sqlite3_bind_parameter_index(ResultStmt, ":sur");
     sqlite3_bind_text(ResultStmt, idx,  CurrentName ->
second.GetSurName().c_str(), -1, SQLITE_STATIC);
     sqlite3_step(...)

When this code will be always correct:

     idx = sqlite3_bind_parameter_index(ResultStmt, ":sur");
     sqlite3_bind_text(ResultStmt, idx,  CurrentName ->
second.GetSurName().c_str(), -1, SQLITE_TRANSIENT);
     sqlite3_step(...)


Pavel

On Sun, Dec 19, 2010 at 5:21 PM, jeff archer <jarch...@yahoo.com> wrote:
>>> My only guess is that basic_string::c_str() doesn't really provide a 
>>> pointer to
>>>a null-terminated c-style string, >but a facsimile of one that SQLite doesn't
>>>like.
>>
>>c_str() provides pointer to the data that string has with additional
>>null byte added at the end. That's it. Whether it is a null-terminated
>>c-style string depends on what data you put into it. If that data has
>>null bytes in it then SQLite will see only part of your string, though
>>I'm not sure that it was the actual problem you have seen.
>
> You may probably already know this but maybe I'll remind you.  The pointer
> returned by c_str() is only valid in the statement where it is used or 
> possibly
> as long as the life of the basic_string<char> it came from.
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@sqlite.org
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to