在MySQL2 gem中使用准备好的语句吗? [英] Using prepared statement with MySQL2 gem?

查看:120
本文介绍了在MySQL2 gem中使用准备好的语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I create prepared statements for insert and select queries in MySQL? I am using the MySQL2 gem, and my connection object looks like this:

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

推荐答案

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

Unfortunately, mysql2 gem does not have prepared statement support yet. The contributors are planning to add such a feature in a near future, as we can see by this Pull Request discussion:

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

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

If you must have prepared statements in your application, I would recommend you to read about Sequel, which has a very nice support for prepared statements and bound variables:

https://github.com/jeremyevans/sequel

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

更新

@lulalala从版本 0.4.0 开始提到 MySQL2 gem 支持准备好的语句:

As mentioned by @lulalala starting on version 0.4.0 MySQL2 gem supports prepared statements:

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

我希望能帮上忙.

这篇关于在MySQL2 gem中使用准备好的语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆