将带有特殊字符(如引号或反斜杠)的字符串存储在PostgreSQL表中 [英] Store string with special characters like quotes or backslash in postgresql table

查看:1204
本文介绍了将带有特殊字符(如引号或反斜杠)的字符串存储在PostgreSQL表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个值字符串


'MAX DATE QUERY:SELECT iso_timestamp(MAX(time_stamp))从观察到的最大时间,其中offing_id ='HOBART''

但是在插入到postgresql表中时错误:

But on inserting into postgresql table i am getting error:


org.postgresql.util.PSQLException:错误: HOBART或附近的语法错误。

org.postgresql.util.PSQLException: ERROR: syntax error at or near "HOBART".

这可能是因为我的字符串包含单引号。我不知道我的字符串值。每次它不断变化时,由于我正在从文件中读取并保存到postgres数据库中,因此可能包含诸如\之类的特殊字符。

This is probably because my string contains single quotes. I don't know my string value. Every time it keeps changing and may contain special characters like \ or something since I am reading from a file and saving into postgres database.

请提供一般的解决方案以进行转义这样的字符。

Please give a general solution to escape such characters.

推荐答案

根据SQL标准,通过将引号加倍来定界,即:

As per the SQL standard, quotes are delimited by doubling them, ie:

insert into table (column) values ('I''m OK')

如果用两个单引号替换文本中的每个引号,它将起作用。

If you replace every single quote in your text with two single quotes, it will work.

通常,反斜杠会转义以下字符,但字面反斜杠也可以通过使用两个反斜杠转义。

Normally, a backslash escapes the following character, but literal backslashes are similarly escaped by using two backslashes"

insert into table (column) values ('Look in C:\\Temp')

这篇关于将带有特殊字符(如引号或反斜杠)的字符串存储在PostgreSQL表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆