How do I create a prepared insert statement in Sequel?

谁都会走 提交于 2019-12-13 17:27:14

问题


I am attempting to create a prepared insert statement in Sequel and I am as far as

db[:registration].prepare(:insert)
=> <Sequel::Mysql2::Dataset/PreparedStatement "INSERT INTO `registration` () VALUES ()">

How do I create a statement that is something like the following:

INSERT INTO `registration` (`name`, `email`) VALUES (?, ?)

The documentation is a little bit obtuse and I can't find any examples online.


回答1:


Figured this out looking at their rspecs:

statement = db[:registration].prepare(:insert, :prepared_statement_name, :email => :$email, :name => :$name)
statement.call(:name => "foo", :email => "foo@bar.com")

NOTE

The keys that are passed to .call correspond to the values passed in the hash in prepare. So this would work too:

statement = db[:registration].prepare(:insert, :prepared_statement_name, :email => :$e, :name => :$n)
statement.call(:n => "foo", :e => "foo@bar.com")



回答2:


ds = db[
         "INSERT INTO `registration` (`name`, `email`) VALUES (?, ?)", 
         name, email
     ]
ds.call(:insert)


来源:https://stackoverflow.com/questions/18797947/how-do-i-create-a-prepared-insert-statement-in-sequel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!