Search code examples
rubypostgresqlsequel

Insert Into DB using Sequel


I want to insert some data into a Postgres DB using Sequel. I have pursued two approaches, none has yielded the results I want.

My first approach was "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

My second approach was "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

Solution

  • Simply following the code below worked for me:

    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
    

    Changing the values for :created_at and :updated_at to store time stamps without time zones did the trick.

    It is weird how Sequel wouldn't output an error to warn about this.