【问题标题】:How to insert into Postgres table with the PG gem如何使用 PG gem 插入 Postgres 表
【发布时间】:2017-07-19 18:15:08
【问题描述】:

我用 Postgres 创建了三个数据库模式。表格肯定在那里,我使用带有\dt 的shell 来查看它们。

尝试使用我的种子.rb 文件进行插入时,我的 SqlRunner 失败。终端在“11.18”处或附近返回 PG::SyntaxError。上一行也是float,所以不知道哪里出了问题。

模型如下:

require('pg')
require('../project_giclee_db/sql_runner')


class Material

  attr_reader :id, :product_name, :guk_name, :roll_width_in,  :roll_length_metres, :list_price, :cost_per_sqm,
    :cost_per_sqm_with_ink, :factor_n, :sell_per_sqm, :rounded_sale_price


  def initialize( options )
    @id = options['id'].to_i
    @product_name = options['product_name']
    @guk_name = options['guk_name']
    @roll_width_in = options['roll_width_in'].to_i
    @roll_length_metres = options['roll_length_metres'].to_i
    @list_price = options['list_price'].to_f
    @cost_per_sqm = options['cost_per_sqm'].to_f
    @cost_per_sqm_with_ink = options['cost_per_sqm_with_ink'].to_f
    @factor_n = options['factor_n'].to_f
    @sell_per_sqm = options['sell_per_sqm'].to_f
    @rounded_sale_price = options['rounded_sale_price'].to_i

  end


  def save
    sql = "INSERT INTO materials (product_name, guk_name, roll_width_in, roll_length_metres,
    list_price, cost_per_sqm, cost_per_sqm_with_ink, factor_n, sell_per_sqm, rounded_sale_price)
    VALUES (#{@product_name}, #{@guk_name}, #{@roll_width_in}, #{@roll_length_metres}, #{@list_price}
    #{@cost_per_sqm}, #{@cost_per_sqm_with_ink}, #{@factor_n}, #{@sell_per_sqm}, #{@rounded_sale_price}
    ) RETURNING *"

    data = SqlRunner.run(sql).first
    @id = data['id']
  end


  def self.delete_all
    sql = "DELETE FROM materials"
    SqlRunner.run(sql)
  end

end

跑步者的样子:

require('pg')

class SqlRunner

  def self.run(sql)
    begin
   db = PG.connect( {dbname: 'giclee_db', host: 'localhost'} )
     result = db.exec(sql)
    ensure
    db.close
  end
   return result
 end

end

种子数据如下:

require('../models/materials')
require('pry-byebug')

Material.delete_all

 @material1 = Material.new( { 'product_name' => 'giclee_canvas',   'guk_name' => 'canvas',
'roll_width_in' => 44, 'roll_length_metres' => 12, 'list_price' => 150.00,
'cost_per_sqm' => 11.18, 'cost_per_sqm_with_ink' => 14.18, 'factor_n' => 11.00,
'sell_per_sqm' => 156.03, 'rounded_sale_price' => 156
} )


@material1.save

【问题讨论】:

  • 如果您不使用 Rails,我强烈建议您在与数据库通信时使用 Sequel。这是一个非常好的 ORM,它将使生成查询或语句变得更加容易,而无需担心语法。 Pg 强制您编写特定于 PostgreSQL 的 SQL。 Sequel 允许您使用 Ruby 生成 SQL,并且与 DBM 无关,supporting many different DBMs,您只需更改您的 DSN 以使其自动为不同的数据库类型生成等效查询。
  • 在 Ruby 中,在 require 参数周围不使用括号是惯用的。 @material1 还表明您不了解 Ruby 中的变量及其作用域。我建议您熟悉类、实例和局部变量以及常量。
  • 好的!看了一下,似乎很有希望。我准备好用 pg 跳火车了,我只想建模一个数据库。稍后我将使用 rails 来构建 API。续集看起来更加用户友好。谢谢@The Tin Man
  • 我承认我对此比较陌生,但你提到的任何事情都不会破坏 SqlRunner。
  • 你指的是风格,我被教导在那里放括号,所以我这样做了;出于习惯。

标签: ruby postgresql pg


【解决方案1】:

你的字符串应该有引号:

def save
 sql = "INSERT INTO materials (product_name, guk_name, roll_width_in, roll_length_metres,
list_price, cost_per_sqm, cost_per_sqm_with_ink, factor_n, sell_per_sqm, rounded_sale_price)
VALUES ('#{@product_name}', '#{@guk_name}', #{@roll_width_in}, #{@roll_length_metres}, #{@list_price}
#{@cost_per_sqm}, #{@cost_per_sqm_with_ink}, #{@factor_n}, #{@sell_per_sqm}, #{@rounded_sale_price}
) RETURNING *"

  data = SqlRunner.run(sql).first
  @id = data['id']
end

【讨论】:

  • 好喊@eiko,但我得到了同样的错误。做出改变,进步就是进步。
【解决方案2】:

问题是SQL中行尾缺少逗号:

VALUES ('#{@product_name}', '#{@guk_name}', #{@roll_width_in}, #{@roll_length_metres}, #{@list_price}

【讨论】:

  • 变量 sql 中的换行符也很糟糕。尝试不换行。 sql = "插入材料 (product_name, guk_name, roll_width_in, roll_length_metres, list_price, cost_per_sqm, cost_per_sqm_with_ink, factor_n, sell_per_sqm, rounded_sale_price) VALUES ('#{@product_name}', '#{@guk_name}', #{@roll_width_in} , #{@roll_length_metres}, #{@list_price}, #{@cost_per_sqm}, #{@cost_per_sqm_with_ink}, #{@factor_n}, #{@sell_per_sqm}, #{@rounded_sale_price} ) 返回 *"
  • 为什么@rafael.cote 不好?
  • 不管怎样,我在热座位上,撬盖是打开的,我有一个返回的对象哈希。谢谢
  • @godhar 我认为“\n”在执行时会出错,但它对我有用。
  • 是的,我基本上只有这种方式,所以我可以在编辑器中看到它。干杯@rafael.cote
猜你喜欢
  • 2016-05-01
  • 2015-09-10
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 2021-04-07
  • 2019-02-28
相关资源
最近更新 更多