防止sqlite转义反斜杠 [英] Preventing sqlite from escaping backslash

查看:1134
本文介绍了防止sqlite转义反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sqlite datbase中有一个varchar列。特定的一行文本有一个反斜杠,与\\\
一起用于UITextView中的回车。当通过SELECT从数据库读取值时:

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"


$ b b

它不解释为回车。如果我在文本中添加两个黑色斜杠,第二个也将被转义,并且当textview作为子视图添加时最终导致异常:

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:

由于未捕获而终止应用程序exception'NSUnknownKeyException',reason:'[setValue:forUndefinedKey:]:这个类不是键值编码兼容的键测试。

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?

推荐答案

反斜杠毫无疑问在列的文本 - - 你看到它加倍,因为这是反斜杠如何显示(避免与转义序列混淆)。 sqlite不会更改您在查询时在其中放置的文本 - 但可以说,数据库中的文本不是您想要接收的文本。

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.

你需要做的是将两个字符backslash-n改为单个字符 newline ,你可以在数据库中全部或每个查询)或在您的目标C代码(每个查询)上,具体取决于您的确切需求。

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.

这篇关于防止sqlite转义反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆