ruby - 在 MySQL2 gem 中使用准备好的语句?

标签 ruby prepared-statement mysql2

如何在 MySQL 中为 insertselect 查询创建准备好的语句?我正在使用 MySQL2 gem,我的连接对象如下所示:

 con = Mysql2::Client.new(:host => "#{ENV['DB_HOST']}", :port => '3306', :username => "#{ENV['DB_UNAME']}", :password => "#{ENV['DB_PWD']}", :database => 'dbname')

最佳答案

不幸的是,mysql2 gem 还没有准备好的语句支持。贡献者计划在不久的将来添加这样的功能,我们可以从这个 Pull Request 讨论中看到:

https://github.com/brianmario/mysql2/pull/289

如果您必须在您的应用程序中有准备好的语句,我建议您阅读有关 Sequel 的内容,它对准备好的语句和绑定(bind)变量有很好的支持:

https://github.com/jeremyevans/sequel

http://sequel.jeremyevans.net/rdoc/files/doc/prepared_statements_rdoc.html

更新

如@lulalala 所述,从版本 0.4.0 开始 MySQL2 gem支持准备好的语句:

statement = @client.prepare("SELECT * FROM users WHERE login_count = ?")
result1 = statement.execute(1) # Binds the value 1 to the placeholder 
result2 = statement.execute(2) # Binds the value 2 to the placeholder

statement = @client.prepare("SELECT * FROM users WHERE last_login >= ? AND location LIKE ?")
result = statement.execute(1, "CA") # Binds 1 and 'CA' to the placeholders, respectively

希望对您有所帮助。

关于ruby - 在 MySQL2 gem 中使用准备好的语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21719003/

相关文章:

ruby - rspec 与不同的 DNS 服务器

ruby-on-rails - has_one,:through => model VS simple method?

ruby-on-rails - rails : configuring a form action's host using custom URL from settings

tomcat - 在 java 中使用 mysql 执行 Preparedstatment 时出错?

PHP - 在 INSERT INTO MySQL 语句中使用变量作为列名的一部分

Mysql2::错误:无法读取目录 - 在 OS X 上从 Homebrew ​​软件重新安装 mysql 后

ruby - 在任何来源中都找不到 ffi

java - 使用PreparedStatements时SQL Server死锁

ruby-on-rails - Redmine - 安装mysql2(0.3.16)时出错,Bundler无法继续

使用 MySQL 5.6 在 CentOS 6.4 x64 上安装 mysql2 gem 失败