Escaping strings

  • Follow


Hi,

This seems rather easy, but its got me stuck.

i am using ruby DBI to insert records into a mysql database.

 dbh.do("INSERT INTO sentences (id,text)
    VALUES
    (#{id},#{sentence})")

the sentence is a pretty long and complex string having many special
characters. I cant seem to escape it out, i tried CGI and the %&&
delimiters, but i cant get it to work.

I appreciate your help!

Thanks!
-- 
Posted via http://www.ruby-forum.com/.

0
Reply krishna.rokhale (3) 3/7/2010 7:23:11 AM

Nvm, got it.

require 'mysql'

   sentence = Mysql.escape_string(sentence.to_s)
    dbh.do("INSERT INTO sentences (id,text)
    VALUES
    (#{id}, '" + sentence + "')")


   :)

-- 
Posted via http://www.ruby-forum.com/.

0
Reply Krishna 3/7/2010 8:14:01 AM


On 03/07/2010 09:14 AM, Krishna Rokhale wrote:
> Nvm, got it.
> 
> require 'mysql'
> 
>    sentence = Mysql.escape_string(sentence.to_s)
>     dbh.do("INSERT INTO sentences (id,text)
>     VALUES
>     (#{id}, '" + sentence + "')")
> 
> 
>    :)
> 

I'd rather use prepared statements with bind variables.  This is much 
safer and also you can offload a bit of work from the database.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
0
Reply Robert 3/7/2010 5:15:02 PM

Robert Klemme wrote:
> On 03/07/2010 09:14 AM, Krishna Rokhale wrote:
>>    :)
>> 
> 
> I'd rather use prepared statements with bind variables.  This is much
> safer and also you can offload a bit of work from the database.
> 
> Kind regards
> 
>   robert


Thanks!
-- 
Posted via http://www.ruby-forum.com/.

0
Reply Krishna 3/7/2010 10:09:54 PM

3 Replies
1703 Views

(page loaded in 1.239 seconds)

Similiar Articles:













7/24/2012 8:58:08 AM


Reply: