I have the following array of strings:
array = [id, message, date, length]
Example:
array = ["1", "test's message", "2016-01-01", "15"]
I want to merge it into one string. I will use that string to insert data in DB using:
ActiveRecord::Base.connection.execute
I did:
result = "(#{array[0}', '#{array[1]}', '#{array[2]}', '#{array[3]}')"
message contains special characters ' (ASCII code 039). This results in SQL exception. How can I construct a result string that includes the ' character?
EDIT:
To put data in BD I use:
conn = ActiveRecord::Base.connection
sql = "INSERT INTO table_name (`id`, `message`, `date`, `length`) VALUES #{result}"
conn.execute sql
EDIT:
I fixed this using AR method:
ActiveRecord::Base.connection.quote(message)
You are doing it wrong. There is 2016 around an no one sane developer would construct SQL by joining strings.Why? I need performance. I want to put data in DB using raw SQL. I don't need AR because I have validated data and I am not scared about Dependency injection because it is simple backgroud job. Tell my why. I have huge amount of data and using plain sql increases inserting 20xAt which point is the exception raised? During loading the file? During generation of the string? During SQL query?exception is rised because I have'char in message string. This is why I am asking how merge strings with special characters. You don't need exception message.'in the message string.