2017-02-28 63 views
0

我使用Postgres创建了三个DB模式。桌子肯定是在那里,我用\dt的壳看到他们。如何使用PG gem插入到Postgres表中

当试图用我的seeds.rb文件进行INSERT时,我的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 
+0

我会强烈建议使用[续](http://sequel.jeremyevans.net)交谈数据库时,如果你没有使用Rails。这是一个非常好的ORM,它可以使生成查询或语句更容易,而无需担心语法。 Pg迫使你编写特定于PostgreSQL的SQL。 Sequel允许你使用Ruby来生成SQL,并且是DBM不可知的,[支持许多不同的DBM](http://sequel.jeremyevans.net/rdoc-adapters/index.html),你只需要改变你的DSN就可以自动生成为不同的数据库类型生成等效的查询。 –

+0

在Ruby中,不需要围绕'require'参数使用圆括号。 '@ material1'也表明你不了解Ruby的变量和范围。我建议熟悉类,实例和局部变量以及常量。 –

+0

好吧!看一看,看起来很有希望。我准备用pg跳火车,我只想模拟一个数据库。稍后我将使用rails来构建API。续集看起来更加用户友好。谢谢@The Tin Man – godhar

回答

0

你应该在你的字符串引号:

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 
+0

好评@eiko,但我得到同样的错误。做出改变,进度正在进步。 – godhar

0

的问题是在最后一个逗号丢失在SQL行:

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

变量sql中的换行符也很糟糕。 尝试不换行。 VALUE('#{产品名称}','#{guk_name}',#{@产品名称,插入物料, #{@ price_per_sqm_with_ink},#{@ sell_per_sqm},#{rounded_sale_price}}返回*“ –

+0

是的。谢谢。 – godhar

+0

为什么它不好@ rafael.cote? – godhar