[英]Ruby SQL Insert using Mysql2 gem
I'm trying to insert into a remote mysql database. 我正在尝试插入远程mysql数据库。 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.
我怀疑它与我的绑定有关,但这几乎与我使用sqlite3所使用的绑定完全相同,我认为使用相同的Arel进行插入。
@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; Mysql2 :: Error:您的SQL语法有错误;
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
我也尝试过使用''封装我要插入的变量
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. 我猜我的代码中有一些奇怪的字符,所以我不得不强制使用UTF-8编码,然后使用下面的gsub删除了这些字符,现在一切都在进行中。
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"))
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.