如何在Sequel中创建准备好的插入语句?

时间:2013-09-14 03:18:56

标签: ruby mysql2 sequel

我正试图在续集中创建一个准备好的insert语句,我就是

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

如何创建类似于以下内容的语句:

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

文档有点迟钝,我在网上找不到任何示例。

2 个答案:

答案 0 :(得分:4)

看看他们的rspecs:

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

注意

传递给.call的密钥对应于prepare中散列中传递的值。所以这也会起作用:

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

答案 1 :(得分:0)

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