查询Postgres数据库(Querying Postgres database)

编程入门 行业动态 更新时间:2024-10-24 22:23:55
查询Postgres数据库(Querying Postgres database)

我想查询一个列匹配字符串的数据库,这个字符串有一个单引号字符'。 我无法运行以下查询

SELECT * from <table> where <column>='robocopy'w (ithout)backup.pcap';

原因是单引号用于搜索列; 我试过双引号但没有帮助。 如果我在字符串中的单个引号之前添加额外的单引号,我可以通过它。

SELECT * from <table> where <column>='robocopy''w (ithout)backup.pcap';

但我需要一个更好的解决方案,因为每次添加单引号是不切实际的,特别是如果它们很长并且有很多单引号。

I want to query a database where a column matches a string & this string is having a single quote character '. I am unable to run the following query

SELECT * from <table> where <column>='robocopy'w (ithout)backup.pcap';

The reason being that single quotes is used to search the column; I tried double quotes but no help. I can by pass this if I add a extra single quote just before the single in a string.

SELECT * from <table> where <column>='robocopy''w (ithout)backup.pcap';

But I need a better solution as it is not practical to add a single quote every time,specially if they are long and have many single quotes.

最满意答案

在将查询字符串发送到数据库之前,需要转义它。 我无法告诉你如何因为你没有发布代码,但如果你直接在psql控制台中运行这个查询,你可以通过将所有约束放在$$和$$中来转义引号,如文档中所引用的那样[1]。 例:

SELECT * from <table> where <column> = $$robocopy'w (ithout)backup.pcap$$;

[1] http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING

You need to escape your query string before sending it to the database. I can't show you how because you didn't post the code, but if you are running this query directly in the psql console, you can escape the quotes by putting all constraint inside $$ and $$, as cited in the documentation[1]. Example:

SELECT * from <table> where <column> = $$robocopy'w (ithout)backup.pcap$$;

[1] http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING

更多推荐

本文发布于:2023-07-15 05:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   Postgres   database   Querying

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!