在PostgreSQL中插入带单引号的文本

本文介绍了如何在PostgreSQL中正确插入包含单引号的文本,讨论了字符串文字、转义机制以及参数化语句的重要性,以避免SQL注入风险。建议使用双引号对单引号进行转义,或者使用美元引号来避免字符串引用问题。
摘要由CSDN通过智能技术生成

本文翻译自:Insert text with single quotes in PostgreSQL

I have a table test(id,name) . 我有一个表test(id,name)

I need to insert values like: user's log , 'my user' , customer's . 我需要插入以下值: user's log'my user'customer's

 insert into test values (1,'user's log');
 insert into test values (2,''my users'');
 insert into test values (3,'customer's');

I am getting an error if I run any of the above statements. 如果我运行以上任何语句,都会收到错误消息。

If there is any method to do this correctly please share. 如果有任何方法可以正确执行此操作,请分享。 I don't want any prepared statements. 我不要任何准备好的陈述。

Is it possible using sql escaping mechanism? 是否可以使用sql转义机制?


#1楼

参考:https://stackoom.com/question/pgCX/在PostgreSQL中插入带单引号的文本


#2楼

According to PostgreSQL documentation (4.1.2.1. String Constants) : 根据PostgreSQL文档(4.1.2.1。字符串常量)

 To include a single-quote character within a string constant, write two 
 adjacent single quotes, e.g. 'Dianne''s horse'.

See also the standard_conforming_strings parameter, which controls whether escaping with backslashes works. 另请参见standard_conforming_strings参数,该参数控制是否使用反斜杠转义。


#3楼

This is so many worlds of bad, because your question implies that you probably have gaping

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值