在 postgresql 中转义单引号
本文讨论如何在 postgresql 查询中转义单引号。
在 postgresql 中转义单引号
考虑一个记录用户评论的评论表。该表有 5 个字段:id
、userid
、postid
、comments
、commentdate
,如下所示:
|id | userid | postid | comments | commentdate
|---|-------- |---------|--------------------------------|---------------------
|1 | 1 | 1 | the post is great | 07-02-2022 11:03:05
|2 | 2 | 1 | we've found the right post | 07-02-2022 01:17:02
|3 | 3 | 3 | i'm working on a related post | 08-02-2022 09:12:17
|4 | 4 | 3 | excellent post | 08-02-2022 12:04:01
|5 | 5 | 4 | the post's title is impressive | 09-02-2022 16:23:09
我们将在上面的示例中创建表。这是评论表的 create
语句:
createtablecomments(idintnotnullgeneratedalwaysasidentity,useridintnotnull,postidintnotnull,commentstextnotnull,commentdatetimestampnotnull,constraintcomment_pkeyprimarykey(id))
创建表后,我们将在上面示例的第一行中插入值。下面是第一行的 insert
语句:
insertintocomments(userid,postid,comments,commentdate)values(1,1,'the post is great','07-02-2022 11:03:05');
此查询插入成功。
接下来,让我们在第二行中插入值。下面是 insert
语句:
insertintocomments(userid,postid,comments,commentdate)values(2,1,'we'vefoundtherightpost', '07-02-202201:17:02');
当我们尝试执行上面的语句时,会抛出一个语法错误,如下所示:
error: syntax error at or near "ve"
line 1: ... postid, comments, commentdate) values (2, 1, 'we've found t...
postgresql 无法理解 we
之后的单词,因为它假定 we
后的单引号表示字符串的结尾。第 3 行和第 5 行将给出类似的错误,因为它们在 comments
字段中都有单引号。
下面是插入示例中所有行的语句:
insertintocomments(userid,postid,comments,commentdate)values(1,1,'the post is great','07-02-2022 11:03:05'),(2,1,'we'vefoundtherightpost', '07-02-202201:17:02'),
(3, 3, 'i'm working on a related post','08-02-2022 09:12:17'),(4,3,'excellent post','08-02-2022 12:04:01'),(5,4,'the post'stitleisimpressive', '09-02-202216:23:09');
上面的语句将给出与仅插入第二行时的错误相同的错误。
解决此问题的一种方法是转义单引号,这可以通过以下方式完成:
- 另一个单引号
- 反斜杠
- 美元引号
在 postgresql 中使用另一个单引号转义一个单引号
可以通过编写单引号后跟要转义的单引号以转义形式指定单引号。此ag捕鱼王app官网的解决方案显示在这里:
insertintocomments(userid,postid,comments,commentdate)values(2,1,'we''ve found the right post','07-02-2022 01:17:02');
将上述语句中的所有单引号转义的语句如下所示:
insertintocomments(userid,postid,comments,commentdate)values(1,1,'the post is great','07-02-2022 11:03:05'),(2,1,'we''ve found the right post','07-02-2022 01:17:02'),(3,3,'i''m working on a related post','08-02-2022 09:12:17'),(4,3,'excellent post','08-02-2022 12:04:01'),(5,4,'the post''s title is impressive','09-02-2022 16:23:09');
输出:
|id | userid | postid | comments | commentdate
|---|-------- |---------|--------------------------------|---------------------
|1 | 1 | 1 | the post is great | 07-02-2022 11:03:05
|2 | 2 | 1 | we've found the right post | 07-02-2022 01:17:02
|3 | 3 | 3 | i'm working on a related post | 08-02-2022 09:12:17
|4 | 4 | 3 | excellent post | 08-02-2022 12:04:01
|5 | 5 | 4 | the post's title is impressive | 09-02-2022 16:23:09
在 postgresql 中使用反斜杠转义单引号
要使用反斜杠转义单引号,你必须在字符串之前放置 e
符号,这是我们示例中的注释,并在要转义的单引号之前放置反斜杠,如下所示:
insertintocomments(userid,postid,comments,commentdate)values(1,1,'the post is great','07-02-2022 11:03:05'),(2,1,e'we\'vefoundtherightpost', '07-02-202201:17:02'),
(3, 3, e'i\'m working on a related post','08-02-2022 09:12:17'),(4,3,'excellent post','08-02-2022 12:04:01'),(5,4,e'the post\'stitleisimpressive', '09-02-202216:23:09');
输出:
|id | userid | postid | comments | commentdate
|---|-------- |---------|--------------------------------|---------------------
|1 | 1 | 1 | the post is great | 07-02-2022 11:03:05
|2 | 2 | 1 | we've found the right post | 07-02-2022 01:17:02
|3 | 3 | 3 | i'm working on a related post | 08-02-2022 09:12:17
|4 | 4 | 3 | excellent post | 08-02-2022 12:04:01
|5 | 5 | 4 | the post's title is impressive | 09-02-2022 16:23:09
在 postgresql 中通过美元引号转义单引号
如果你想要一个更具可读性的ag捕鱼王app官网的解决方案,特别是当存在多个单引号时,可以使用美元引号。
如果字符串中有更多单引号,则美元引号可以使ag捕鱼王app官网的解决方案可读。美元引用使用美元符号、可选标记、字符串(在本例中为注释),然后是另一个美元符号、可选标记和结束美元符号。
单引号可以在用美元引用的字符串中使用,而不会被转义。可以使用美元引用插入一行,如下所示:
insertintocomments(userid,postid,comments,commentdate)values(6,5,$$'i'vesharedthepost.it's quite impressive'$$,'09-02-2022 16:34:17')
这是以了解有关 postgresql 字符串常量及其转义的更多信息。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在一个 postgresql 查询中使用多个 with 语句
发布时间:2023/03/20 浏览次数:337 分类:postgresql
-
在本教程中,我们将学习如何使用多个 with 语句在 postgresql 中使用两个临时表执行查询。
发布时间:2023/03/20 浏览次数:185 分类:postgresql
-
本文介绍如何在 ubuntu 上找到 postgresql 数据库的配置文件。
发布时间:2023/03/20 浏览次数:409 分类:数据库
-
本文解释了如何直接从终端/命令行或 psql shell 运行 sql 文件。为此,你需要指定主机名、端口、用户名和数据库名称。
发布时间:2023/03/20 浏览次数:89 分类:postgresql
-
本文展示了如何列出 postgresql 上的活动连接。
发布时间:2023/03/20 浏览次数:966 分类:postgresql
-
在 pl/sql 中,你可能需要在 postgres 中使用循环。我们可以使用 for 和 while 语句来创建循环。
发布时间:2023/03/20 浏览次数:141 分类:postgresql
-
本文介绍如何在 postgresql 中仅使用单个查询来重命名列以及更改其类型。
发布时间:2023/03/20 浏览次数:233 分类:postgresql
-
本文介绍如何在 postgresql 中使用 select 方法连接列。
发布时间:2023/03/20 浏览次数:281 分类:postgresql
-
本文展示了如何使用 case 语句并给出了 postgresql 中的示例。