gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 07:48:03 26 4
gpt4 key购买 nike

如何在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com