Reputation: 6075
I'm trying to insert into a remote mysql database. I am able to connect correctly and can query 'select' no problem from it. However, I cannot perform inserts into the same table that I can select from. I suspect it has something to do with my binds, but this is nearly identical to what I was using to get sqlite3 working which I think uses the same Arel to insert.
@result = @db.query("insert into lead_to_processes (case_number, style_of_case) values (?,?)", [
self.case_number.to_blob.force_encoding("UTF-8"),
self.style_of_case.to_blob.force_encoding("UTF-8")
]
)
Ultimate goal is to be able query a remote database from inside of a model and insert data into it. I've tried using Octopus and that didn't quite work because the tables will be different from the databases.
I have full permissions with this user on the database.
So following guidance from comments i changed the syntax and am getting a different error
Mysql2::Error: You have an error in your SQL syntax;
However i'm doing the query like this now
@db = Mysql2::Client.new(connectionstring)
@case_number = @db.escape(self.case_number)
@style_of_case = @db.escape(self.style_of_case)
@db.query("insert into lead_to_processes (case_number, style_of_case) VALUES
(#{@case_number}, #{@style_of_case})
Any ideas or guidance? I've also tried this with '' encapsulating the variables that i'm inserting
Upvotes: 2
Views: 8628
Reputation: 6075
I guess there were some weird characters in my code so I had to force UTF-8 encoding and then removed the characters using gsub below, everything is flowing now.
Thanks for the advice
@db.escape(self.style_of_case.force_encoding("UTF-8"))
@db.escape(self.case_number.gsub(/[\xC2]/,'').gsub(/[\xA0]/,'').force_encoding("UTF-8"))
Upvotes: 1
Reputation: 23
Is it possible that you are missing an end quote?
this
@db.query("insert into lead_to_processes (case_number, style_of_case) VALUES
(#{@case_number}, #{@style_of_case})
should be
@db.query("insert into lead_to_processes (case_number, style_of_case) VALUES
(#{@case_number}, #{@style_of_case}") <== notice the quote at the end.
Upvotes: 0