我有一个返回c std :: string的方法,然后在将它传递给sqlite3_bind_text之前将其转换为c_str().我的问题是,这应该使用SQLITE_STATIC还是SQLITE_TRANSIENT?
sqlite3_bind_text(insertStatement, 0, suspect->GetIpString().c_str(), -1, SQLITE_STATIC);
// Do some stuff in same function then sqlite3_step
sqlite3_bind_text的文档说,
The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
string after SQLite has finished with it. The destructor is called to
dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
sqlite3_bind_text(), or sqlite3_bind_text16() fails. If the fifth
argument is the special value SQLITE_STATIC, then SQLite assumes that
the information is in static, unmanaged space and does not need to be
freed. If the fifth argument has the value SQLITE_TRANSIENT, then
SQLite makes its own private copy of the data immediately, before the
sqlite3_bind_*() routine returns.
GetIpString返回一个std :: string,但如果我没有明确地复制它,那么该字符串会在调用后继续存在吗?另外,在没有SQLITE_TRANSIANT的情况下使用.c_str()是否安全?我知道SQLITE_TRANSIENT是安全的选择,但如果不需要,我想避免复制/性能命中,因为这个查询将会运行很多.
解决方法:
在您的情况下,GetIpString()返回的字符串对象很可能在查询执行和最终确定之前被破坏,因此您应该使用SQLITE_TRANSIENT.
一般情况下,您不应该为临时副本而烦恼,除非您有实际的性能问题,而这些问题是由此引起的.
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!