如何使用mysql2 gem创建准备好的语句? [英] How do you create prepared statements with the mysql2 gem?

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

问题描述

我尝试使用Google回答这个看似简单的问题,但令我惊讶的是,它没有帮助.

I've tried using google to answer this seemingly simple question, but to my surprise, it didn't help.

我的Rails应用程序中有当前使用mysql gem的准备"方法的代码.切换到mysql2时,这会因错误而中断:

I have code in my rails application currently using the 'prepare' method with the mysql gem. On switching to mysql2, this breaks with the error:

undefined method `prepare' for #<Mysql2::Client::0.......

因此,我尝试查找"prepare"方法的版本,但到目前为止该搜索未成功.有人可以帮我吗?

So I tried looking for a version of the 'prepare' method but this search has been unsuccessful so far. Can anyone help me out with this?

如果不可能,是否有人可以通过mysql2库中的内容简单地对我的查询进行参数化设置?

If this isn't possible, could anyone let me know if there's a way to simply parameterize my queries with something in the mysql2 library?

推荐答案

mysql2 gem现在根据语法如下:

statement = @client.prepare("SELECT * FROM users WHERE login_count = ?")
result1 = statement.execute(1)
result2 = statement.execute(2)

在2015年6月添加了,并添加了合并拉取请求.

This was added with a merged pull request in June 2015.

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

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