来自哈希的PG多个插入语法

时间:2018-08-01 16:32:05

标签: ruby postgresql pg

我需要执行多个INSERT命令,该命令的数据源最初是散列,并通过PG gem。是否可以使用PG方法来正确格式化数据,还是我必须自己编写此方法?

{:company_id=>"wuoyna",
 :products=>
  [{:Item=>"Leaf Lettuce",
    :Unit=>"ea",
    :Price=>2.2,
    :URL=>"www.example.com/products/5505"},
   {:Item=>"Grannysmith Apples",
    :Unit=>"kg",
    :Price=>3.2,
    :URL=>"www.example.com/products/124"},
   {:Item=>"Whole Chicken",
    :Unit=>"ea",
    :Price=>11.0,
    :URL=>"www.example.com/products/397"},
   {:Item=>"Alpha", :Unit=>"kg", :Price=>4.5, :URL=>""},
   {:Item=>"Bravo", :Unit=>"kg", :Price=>5.59, :URL=>""},
   {:Item=>"Charlie", :Unit=>"litre", :Price=>7.0, :URL=>""}]}

...到...

INSERT INTO mytable (name, unit, price, url, key_id, ) VALUES (
  ('Leaf Lettude', 'ea', '2.2', 'www.example.com/products/5505'),
  ('Grannysmith Apples', 'kg', '3.2', 'www.example.com/products/124'),
  ('Whole Chicken', 'ea', '11.0', 'www.example.com/products/397'),
  ('Alpha', 'kg', '4.5', ''),
  ('Bravo', 'kg', '5.59', ''),
  ('Charlie', 'litre', '7.0', '')
  )

我知道有些方法将数组作为参数,以针对PG格式的SQL的其他方面进行正确格式化。我以为我可能会缺少一些用于格式化哈希(实际上是哈希数组)的东西

0 个答案:

没有答案