views:

669

answers:

1

I have a varchar column in an sqlite datbase. A particular row of text has a backslash, which is used with "\n" for a carriage return in a UITextView. When the value is read from the database via a SELECT:

myobject.text = [NSString stringWithUTF8String: (char *)sqlite3_column_text(selectstmt, 2)];

The backslash is escaped and looks like:

"\\n" rather than "\n"

which doesn't interpret as a carriage return. If I add two blackslashes in the text, the second will also be escaped and eventually leads to an exception when the textview is added as a subview:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key test.'

Is there a way to prevent these escapes or will I need to clean it up manually?

+2  A: 

The backslash is no doubt right there in the column's text -- you're "seeing" it as doubled because that's how backslashes are shown (to avoid confusion with escape sequences). sqlite is not altering the text you put there when you query it -- but arguably, the text that's in the database is not what you want to receive.

What you need to do is to change the two characters backslash-n into the single character newline, and you can to that either in the database (once and for all, or on each query) or in your objective-C code (on each query) depending on your exact needs.

Alex Martelli
What do you mean by "single character newline"? I see what you are saying about the double backslashes. However, if I take the same text in the database and assign it to a UITextView directly as a test, it will render with carriage returns. Coming out of the database displays the literal "\n" and no carriage return. What is the difference?
4thSpace
newline is one character, ascii 10; it's often entered in a string literal as the two characters backslash and n, in what's known as an "escape sequence", and strings containing newlines often display them as the escape sequence (depending how you display them). What I'm saying is that you're probably NOT taking "the same text in the database" -- you're taking text with a newline character, which you are entering as an escape sequence. That's probably related to how the two-charater sequences ended up in the DB in the first place, depending how you INSERTed them.
Alex Martelli
I was typing in "\n" manually into the DB via my INSERT statement. However, I've found that just hitting the return key in the text where I want a carriage return will carry through when it comes out of the DB.
4thSpace
Good! Clearly when you were typing \n nothing was "interpreting" it as an escape sequence (and similarly nothing "interpreted" the CR as meaning anything other than itself). Problem solved, then? Or do you need help in fixing stuff that's already in your DB? E.g. you could do a REPLACE "manually into the DB", if needed.
Alex Martelli
Problem definitely solved. Just hitting the return key is the simplest case and all I need. Thanks.
4thSpace