s.gsub!("'", "\\\\'")
should do the trick.
Regards,
Stefan
--
Posted with http://DevLists.com. Sign up and save your time!
s.gsub!(/'/, "\\\\'")
or
s.gsub!(/'/) { "\\'" }
will do what you want
Hope this helps,
Mike
--
Mike Stok <mi...@stok.co.uk>
http://www.stok.co.uk/~mike/
The "`Stok' disclaimers" apply.
What interface to MySQL are you using? I know that the Ruby DBI
module allows you to use place-holders in queries, and the DBI layer
does the escaping for you e.g.
dbh.do("INSERT INTO people (id, name, height) VALUES(?, ?, ?)",
nil, "Na'il", 76)
The resulting statement produced by do and sent to the server looks
like this:
INSERT INTO people (id,name,height) VALUES(NULL,'Na\'il',76)
(stolen from http://www.kitebird.com/articles/ruby-dbi.html) or if
you are using the ruby mysql interface then (from http://
www.kitebird.com/articles/ruby-mysql.html)
Using escape_string, the platypus record might be inserted as
follows:
name = dbh.escape_string("platypus")
category = dbh.escape_string("don't know")
dbh.query("INSERT INTO animal (name, category)
VALUES ('" + name + "','" + category + "')")
Mike Stok a écrit :
> Thank you very much for these four answers, but curiously none
> works on my box ! :
Incorrect. They all work.
p "j\\'ai"
"j\\'ai"
puts "j\\'ai"
j\'ai
> Stefan Lang : s.gsub!("'", "\\\\'") gives "j\\'ai" not "j\'ai"
puts "j'ai".gsub("'", "\\\\'")
j\'ai
> Timothy Hunter : s.sub(/'/) { '\\\'' } gives "j\\'ai"
puts "j'ai".gsub(/'/) { '\\\'' }
j\'ai
> you : s.gsub!(/'/, "\\\\'") gives "j\\'ai"
puts "j'ai".gsub(/'/, "\\\\'")
j\'ai
> and : s.gsub!(/'/) { "\\'" } gives "j'ai"
puts "j'ai".gsub(/'/) { "\\'" }
j\'ai
--
Eric Hodel - drb...@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant
Alain FELER a écrit :