插入数据库时在数据中包含单引号
Including single quote in data while inserting to database
我有一个字符串"I love McDonald's burgers. it's the best."
我想将它插入到一个列中,将它们分成 15 个字符串。
因此我需要将结果作为字符串插入 3 行
I love McDonald
's burgers. it'
s the best.
但是如果我使用' '来包含',字符串中会出现一个额外的',这将影响我计算 15 个字符的破损率。
是否有任何其他方式来包含“而不必再使用一个”来转义它?
请帮忙。
如果要将字符串分解为变量,则不需要添加额外的 ':
DECLARE
mcdonald_string VARCHAR2(50) := 'I love McDonald''s burgers. it''s the best.';
BEGIN
WHILE LENGTH(mcdonald_string) > 0 LOOP
INSERT INTO your_table(your_field) VALUES (SUBSTR(mcdonald_string,1,15));
mcdonald_string := SUBSTR(mcdonald_string,16);
END LOOP;
COMMIT;
END;
Doubling the quotation marks within a complicated literal,
particularly one that represents a SQL statement, can be tricky. You
can also use the following notation to define your own delimiter
characters for the literal. You choose a character that is not present
in the string, and then do not need to escape other single quotation
marks inside the literal:
-- q'!...!' notation allows the of use single quotes
-- inside the literal
string_var := q'!I'm a string, you're a string.!';
http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/fundamentals.htm#sthref339
我有一个字符串"I love McDonald's burgers. it's the best."
我想将它插入到一个列中,将它们分成 15 个字符串。
因此我需要将结果作为字符串插入 3 行
I love McDonald
's burgers. it'
s the best.
但是如果我使用' '来包含',字符串中会出现一个额外的',这将影响我计算 15 个字符的破损率。
是否有任何其他方式来包含“而不必再使用一个”来转义它?
请帮忙。
如果要将字符串分解为变量,则不需要添加额外的 ':
DECLARE
mcdonald_string VARCHAR2(50) := 'I love McDonald''s burgers. it''s the best.';
BEGIN
WHILE LENGTH(mcdonald_string) > 0 LOOP
INSERT INTO your_table(your_field) VALUES (SUBSTR(mcdonald_string,1,15));
mcdonald_string := SUBSTR(mcdonald_string,16);
END LOOP;
COMMIT;
END;
Doubling the quotation marks within a complicated literal, particularly one that represents a SQL statement, can be tricky. You can also use the following notation to define your own delimiter characters for the literal. You choose a character that is not present in the string, and then do not need to escape other single quotation marks inside the literal:
-- q'!...!' notation allows the of use single quotes
-- inside the literal
string_var := q'!I'm a string, you're a string.!';
http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/fundamentals.htm#sthref339