Escaping strings

  • Thread starter Krishna Rokhale
  • Start date
K

Krishna Rokhale

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!
 
K

Krishna Rokhale

Nvm, got it.

require 'mysql'

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


:)
 
R

Robert Klemme

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
 
K

Krishna Rokhale

Robert said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top