如何在Postgresql中使用引号转义和替换文本?

时间:2014-09-15 04:10:54

标签: sql ruby-on-rails postgresql ruby-on-rails-4 escaping

我有一个使用Postgresql的Rails 4.1应用程序。我想替换我的数据库中的所有以下文本:

'100': 'hello world

用这个:

'100': 'greetings!\\n hello world

这是我尝试逃避和替换文本的一种方法:

query = "update notes set text = replace(text, \"'100': 'hello world\", \"'100': 'greetings!\\n hello world'\" )"
ActiveRecord::Base.connection.execute(query)

我尝试了几种方法,但它们没有用。我怎样才能正确地逃避和替换文本?

1 个答案:

答案 0 :(得分:0)

我最终在ruby而不是SQL中执行此操作。这是我创建的功能:

def replace_text(id)
  note = Note.find(id)
  note.text = note.text.gsub(/'100'\s{0,1}:\s{0,1}'hello world/, "'100': 'greetings!\\nhello world")
  note.save
end