最近我在写SQL插入到Postgresql数据库中时候,遇到字符串中带有单引号,然后出错,就需要转义之后才能插入。
数据表
CREATE TABLE "test"."aa" (
"id" int4 NOT NULL,
"name" varchar(255) COLLATE "pg_catalog"."default",
CONSTRAINT "aa_pkey" PRIMARY KEY ("id")
)
;
ALTER TABLE "test"."aa"
OWNER TO "postgres";
插入语句
insert into test.aa VALUES(1,''test-name'')
如果字符串带单引号,就会SQL报错,所以需要转义才能插入
insert into test.aa VALUES(1,'''test-name''')
MySQL则有以下方式可以插入
insert into test.aa VALUES(1,'''test-name''')
insert into test.aa VALUES(1,'\'test-name\'')
在PostgreSQL中,当尝试插入含有单引号的字符串时,需要进行转义以避免SQL语法错误。例如,插入语句`insertintotest.aaVALUES(1,test-name)`会失败,正确的方式是使用两个单引号进行转义,如`insertintotest.aaVALUES(1,test-name)`。MySQL提供了类似的方法,可以使用反斜杠转义,如`insertintotest.aaVALUES(1, est-name)`。
1466

被折叠的 条评论
为什么被折叠?



