Ruby 如何使用PG gem插入Postgres表

Ruby 如何使用PG gem插入Postgres表,ruby,postgresql,pg,Ruby,Postgresql,Pg,我用Postgres创建了三个DB模式。表肯定在那里,我使用shell和\dt查看它们 尝试使用seeds.rb文件进行插入时,我的SqlRunner失败。终端返回位于或接近“11.18”的PG::SyntaxError。前一行也是浮动的,所以我不知道怎么了 模型如下所示: require('pg') require('../project_giclee_db/sql_runner') class Material attr_reader :id, :product_name, :gu

我用Postgres创建了三个DB模式。表肯定在那里,我使用shell和
\dt
查看它们

尝试使用seeds.rb文件进行插入时,我的SqlRunner失败。终端返回位于或接近“11.18”的PG::SyntaxError。前一行也是浮动的,所以我不知道怎么了

模型如下所示:

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
Runner看起来像:

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
种子数据如下所示:

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

字符串周围应该有引号:

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

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

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

喊得好@eiko,但我也犯了同样的错误。进行了更改,进步就是进步。如果您不使用Rails,我强烈建议在与数据库交谈时使用。这是一个非常好的ORM,它将使生成查询或语句变得更加容易,而无需担心语法。Pg强制您编写特定于PostgreSQL的SQL。Sequel允许您使用Ruby生成SQL,并且与DBM无关,您只需更改DSN,使其自动为不同的数据库类型生成等效查询。在Ruby中,习惯用法是在
require
参数周围不使用括号。另外,
@material1
表明您不了解Ruby中的变量及其作用域。我建议大家熟悉类、实例、局部变量和常量。好的!看了一眼,似乎很有希望。我已经准备好和pg一起训练了,我只想建立一个数据库模型。稍后我将使用rails构建API。续集看起来更加用户友好。Thank@The Tin ManI我承认我对这方面比较陌生,但是你提到的任何事情都不会破坏SqlRunner;出于习惯。变量sql中的换行符也不好。尽量不要断线。sql=“插入到材料(产品名、古克名字、古克名字、卷宽度宽度、卷长度米、卷长度米、列表价格、成本、每平方米成本、每平方米成本、每平方米成本、成本和墨水、因素n、因素n、每平方米成本、每平方米售价、每平方米出售、每平方米出售、每平方米出售、名字、长度米、长度米、长度米、米、价格、标价、标价、价格、价格、价格、价格(价值值)值(“\35\35;#35;{35;{##{{{{{{{{\\#{{\#{{{{{产品名名名长度米、名称米米米米米米米、价格、每平方米、每平方米成本、成本、成本、成本、每平方米每平方米、成本、成本、成本、成本、成本、成本、每平方米和每平方米{@cost_per_sqm_(含墨水)},{@factor_n}#{@sell_per_sqm},{@rounded_sale_price})RETURNING*“为什么它不好@rafael.cote?不管怎么说,我现在很尴尬,撬盖打开了,我有一个返回的对象哈希。Thanks@godhar我以为“\n”在执行时会出现错误,但它对我有效。是的,我基本上只有这样才能在编辑器中看到它。Cheers@rafael.cote