且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQLite中插入新行("\ n")字符?

更新时间:2023-12-04 11:13:46

在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');";