I’d like to know how to make work newline control character inside c-string returned by a sqlite3_column_text. For example, this is the text returned from a query to a sqlite database:
"Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing elit."
and this is the code I use to get it:
char *body = (char *)sqlite3_column_text(statement, 1);
NSString *str = (body)?[NSString stringWithUTF8String:body]:@"";
The problem comes when I try to print it, because in return this:
Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing elit.
instead of:
Lorem ipsum
dolor sit amet,
consectetur
adipiscing elit.
Using the same text but returned from a string column in Core Data works as intended, but I don’t know why is doesn’t in sqlite3. I hope someone can give directions because is quite frustrating.
Looking in the literal values section of the documentation of SQLite I found that the reason of why it return the two-character
\ninstead ofnewlineis because C-style escapes using the backslash character are not supported because they are not standard SQL. So I tried a different aproach by using NSString instead of using SQLite directly:Thanks to this two posts, I get the idea of using
\\nto replace\ncorrectly in the final NSString, so is possible to use other valid control characters.