I\'ve set up swift project to use sqlite. sometimes, when inserting it doesn\'t actually insert the correct (or all) the values. I know because I restart the app, and when
This behavior actually does conform to the specification and the bug is in your code.
The source of your problem is in the swift String you are passing to sqlite3_bind_text. sqlite3_bind_text accepts the text as a const char* which is bridged as UnsafePointer in Swift. The behavior when passing a Swift String to a function that accepts UnsafePointer is documented in the "Constant Pointers" section of the Interacting with C APIs. It says:
The string will automatically be converted to UTF8 in a buffer, and a pointer to that buffer is passed to the function.
Which is probably what you want to have happen.
BUT it also says:
The pointer passed to the function is guaranteed to be valid only for the duration of the function call. Don’t try to persist the pointer and access it after the function has returned.
This is the source of your bug. sqlite3_bind_text() does in fact persist the pointer in the prepared statement possibly until sqlite3_finalize() is called so that it can use it in future sqlite3_step() calls.
Another answer suggested using NSString.UTF8String. This has a lifetime that is a little longer and seems like it should be enough. The documentation says:
This C string is a pointer to a structure inside the string object, which may have a lifetime shorter than the string object and will certainly not have a longer lifetime. Therefore, you should copy the C string if it needs to be stored outside of the memory context in which you use this property.
"memory context" here seems vague. I'm not sure if it means the current function, or until the current autoreleasepool is drained, or something else. If it is one of the first two, you are safe. If not, well I would say you are still safe because it seems like NSString.UTF8String has been used in situations like these for a long time without issues..