2

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

6
  • Do you get any errors? Could you post them? Do you have write permissions for that database? Or only read? Commented Apr 9, 2013 at 13:24
  • I don't believe that the mysql2 gem provides binds for queries, you have to create valid SQL yourself to pass to the query. github.com/brianmario/mysql2 Commented Apr 9, 2013 at 13:35
  • Can you paste the exception which gets thrown? Commented Apr 9, 2013 at 13:39
  • TypeError: can't convert Array into Hash Commented Apr 9, 2013 at 14:24
  • you don't need [] get rid of them Commented Apr 9, 2013 at 14:43

2 Answers 2

1

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"))
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

thanks for response, posted answer as you were posting this, there were garbage characters in the data that was causing the error. Thanks for the help though @Karl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.