使用Sequel插入数据库

时间:2016-09-07 13:02:39

标签: ruby postgresql sequel

我想使用Sequel将一些数据插入到Postgres数据库中。我已经采用了两种方法,没有一种方法能够得到我想要的结果。

我的第一个方法是“Querying in Sequel”:

connection = Sequel.connect('postgres://admin:admin@localhost:5432/test_tcp')    
insert_values = connection["INSERT INTO cards (:card_number, :phone_number, :uuid, :created_at, :updated_at) VALUES (?)", '766877868', '256700000000', '9043', '2016-09-07 11:11:31 +0300', '2016-09-07 11:11:31 +0300']
insert_values.insert
connection.close

我的第二种方法是“Inserting Records”:

connection = Sequel.connect('postgres://admin:admin@localhost:5432/test_tcp')
cards = connection.from(:cards)
cards.insert(:id => 1, :card_number => "13668389", :phone_number => "256700000000", :uuid => "9014", :created_at => '2016-09-07 11:11:31 +0300', :updated_at => '2016-09-07 11:11:31 +0300')
connection.close

1 个答案:

答案 0 :(得分:1)

只需按照以下代码为我工作:

connection = Sequel.connect('postgres://admin:admin@localhost:5432/test_tcp')
cards = connection.from(:cards)
cards.insert(id: 1, card_number: "13668389", phone_number: "256700000000", uuid: "9014", created_at: '2016-09-07 11:11:31', updated_at: '2016-09-07 11:11:31')
connection.close

更改:created_at:updated_at的值以存储没有时区的时间戳就行了。

Sequel不会输出错误来警告这一点,这很奇怪。