如何在SQLite中插入新行("\ n")字符? [英] How to insert a new line ("\n") character in SQLite?

查看:237
本文介绍了如何在SQLite中插入新行("\ n")字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试插入类似内容时:

While trying to insert something like:

"Hello\nWorld"

SQLite会引发类似以下的错误:

SQLite throws error something like:

消息:无法识别的令牌:'Hello";"(还有其他一些错误)

Message: unrecognized token: "'Hello";" (also few other errors)

即使我将上面的字符串转换为"Hello''\nWorld""Hello\"\n\"World",这些转义字符序列在这种情况下也不起作用.

Even though I convert above string to "Hello''\nWorld" or "Hello\"\n\"World", these escape characters sequences don't work in this case.

当前使用C ++语言,我将像插入其他任何简单字符串列一样插入此列.我尝试了以上转义序列(即使我在Internet上阅读过,但它们不适用于\n).

Currently using C++ language, I am inserting this column like any other simple string columns. I tried above escape sequence (even though I read in internet that, they don't work with \n).

如何在SQLite DB中插入换行符和其他此类特殊字符?

How to insert new line and other such special characters in SQLite DB?

推荐答案

在SQL中,没有逃脱换行符的机制.您必须按原样插入它们:

In SQL, there is no mechanism to escape newline characters; you have to insert them literally:

INSERT INTO MyTable VALUES('Hello
world');

或者,动态构造字符串:

Alternatively, construct the string dynamically:

INSERT INTO MyTable VALUES('Hello' || char(10) || 'world');

(换行符的类型(13或10或13 + 10)取决于操作系统.

(The type of newline (13 or 10 or 13+10) is OS dependent.)

将SQL语句嵌入C ++字符串时,在第一种情况下必须转义换行符:

When you embed the SQL statements in C++ strings, you have to escape the newline in the first case:

q1 = "INSERT INTO MyTable VALUES('Hello\nworld');";
q2 = "INSERT INTO MyTable VALUES('Hello' || char(10) || 'world');";

这篇关于如何在SQLite中插入新行("\ n")字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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