-1

I have ruby script that includes a mysql insert that is working fine until it gets to a row that contains data containing an apostrophe. This row is also being populated using a variable and Im unsure how to escape the character so the insert will work successfully.

Any ideas?

lcm
  • 1,737
  • 6
  • 17
  • 40

1 Answers1

1

Use the quote method on the connection object:

quote(value, column = nil)

API Documentation Link

Quotes the column value to help prevent SQL injection attacks. Example:

my_name    = ActiveRecord::Base.connection.quote("John O'Neil")
my_address = ActiveRecord::Base.connection.quote("R'lyeh")

query = "INSERT INTO companies (name,address) VALUES (#{my_name}, #{my_address})"

ActiveRecord::Base.connection.execute(query);

Original Post:

See this post: Escaping a single quotation within SQL query

Community
  • 1
  • 1
Md Sirajus Salayhin
  • 4,974
  • 5
  • 37
  • 46