如何在 sqlite3 文本列返回的 c 字符串内进行换行?

发布于 2024-11-10 11:37:32 字数 637 浏览 2 评论 0原文

我想知道如何在 sqlite3_column_text 返回的 c 字符串中使用换行控制字符。例如,这是从对 sqlite 数据库的查询返回的文本:

"Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing elit."

这是我用来获取它的代码:

char *body = (char *)sqlite3_column_text(statement, 1);
NSString *str = (body)?[NSString stringWithUTF8String:body]:@"";

当我尝试打印它时,问题就出现了,因为返回的是:

Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing elit.

而不是:

Lorem ipsum
dolor sit amet,
consectetur
adipiscing elit.

使用相同的文本但是从 Core Data 中的字符串列返回按预期工作,但我不知道为什么在 sqlite3 中不行。我希望有人能指点迷津,因为这很令人沮丧。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴骨ら 2024-11-17 11:37:33

查看 SQLite 文档的 文字值 部分,我发现它返回的原因使用两个字符的 \n 而不是 newline 是因为使用反斜杠字符的 C 风格转义符不受支持,因为它们不是标准 SQL。所以我尝试了一种不同的方法,使用 NSString 而不是直接使用 SQLite:

char *body = (char *)sqlite3_column_text(statement, 1);
NSString *str = (body)?[NSString stringWithUTF8String:body]:@"";
NSString *container = [str stringByReplacingOccurrencesOfString:@"\\n"    
                                                     withString:@"\n"];

感谢 this 两篇帖子,我想到了使用 \\n 在最终的 NSString 中正确替换 \n ,因此可以使用其他有效的控制字符。

Looking in the literal values section of the documentation of SQLite I found that the reason of why it return the two-character \n instead of newline is 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:

char *body = (char *)sqlite3_column_text(statement, 1);
NSString *str = (body)?[NSString stringWithUTF8String:body]:@"";
NSString *container = [str stringByReplacingOccurrencesOfString:@"\\n"    
                                                     withString:@"\n"];

Thanks to this two posts, I get the idea of using \\n to replace \n correctly in the final NSString, so is possible to use other valid control characters.

小帐篷 2024-11-17 11:37:33

问题在于 sqllite DB 中存储的值是两个字符的 \n 而不是 newline 本身。在将字符串添加到数据库之前,您必须将 \n 转换为换行符 (ASCII 10),或者在 Objective-C 代码中的 SELECT 查询之后处理该字符串。

有关详细信息,请参阅以下答案:

编程时换行符出现问题对于 iPhone

防止 sqlite转义反斜杠

The issue is that the value stored in the sqllite DB is the two-character \n and not the newline itself. You will have to either convert the \n into the newline (ASCII 10) character before adding the string to the DB or process the string after the SELECT query in your Objective-C code.

See the following answers for further info:

Trouble with newline character while programming for the iPhone

Preventing sqlite from escaping backslash

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文