문제

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.

도움이 되었습니까?

해결책

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")

다른 팁

ds = db[
         "INSERT INTO `registration` (`name`, `email`) VALUES (?, ?)", 
         name, email
     ]
ds.call(:insert)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top