问题
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