如何创建准备好的语句insert
and select
MySQL 中的查询?我正在使用 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 https://github.com/brianmario/mysql2/pull/289
If you must如果您的应用程序中有准备好的语句,我建议您阅读 Sequel,它对准备好的语句和绑定变量有非常好的支持:
https://github.com/jeremyevans/sequel https://github.com/jeremyevans/sequel
http://sequel.jeremyevans.net/rdoc/files/doc/prepared_statements_rdoc.html http://sequel.jeremyevans.net/rdoc/files/doc/prepared_statements_rdoc.html
UPDATE
正如@lulalala 所提到的,从版本开始0.4.0 MySQL2 宝石 https://github.com/brianmario/mysql2支持准备好的语句:
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
我希望这有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)