如何创建在续集准备插入语句?(How do I create a prepared insert s

2019-10-18 15:34发布

我试图创建一个准备insert的续集发言,我就

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

如何创造一个类似如下的声明:

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

该文档是有点迟钝,我不能找到任何的例子。

Answer 1:

想通了这一点看着自己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")


Answer 2:

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


文章来源: How do I create a prepared insert statement in Sequel?