0

我正在尝试迭代从我的数据库中提取的食谱,从每个食谱中解析成分并将成分插入到他们自己的表中。问题是我不知道如何在我的内部选择中包含当前的成分变量:目前我收到以下错误:

ExtractIngredients.rb:21:在“查询”中:“字段列表”中的未知列“ing”(Mysql::Error)

你能告诉我如何在我的选择语句中包含“ing”吗?

谢谢,李

begin

db = Mysql.new 'localhost', 'root', 'pass', 'recs'
results = db.query "SELECT id, freeText FROM recipes"

nRows = results.num_rows

for i in 0..nRows-1
    curRow = results.fetch_row
recipeInd = curRow[0]
    recipeFreeText = curRow[1]

    ingredients = getIngredients(recipeFreeText).to_s.scan(/\'(\w+)\'/).flatten

    ingredients.each do |ing|
            db = Mysql.new 'localhost', 'root', 'pass', 'recs'

            # In the next awful select statement I'm trying to insert the current ingredient to the ingredient table if it isn't already there
         db.query "INSERT INTO ingredient(name, created_at, updated_at) SELECT ing, created_at, updated_at FROM dummytable WHERE (SELECT count(*) FROM ingredient WHERE Name = ing)=0"

        ingInd = db.query "SELECT id FROM ingredient WHERE name=ing"
            db.query "INSERT INTO ingredients_recipes(ingredient_id, recipe_id) ingInd, recipeInd"
    end     
end

end
4

2 回答 2

1

像任何其他 Ruby 字符串一样使用字符串插值:#{ing}

也就是说,如果这是 Rails,你为什么要做这样的事情?并为每种成分创建一个新的 MySQL?其中大部分都包含在框架中。

于 2012-04-29T19:57:57.010 回答
0

通过使用插值

"..... WHERE Name = '#{ing}' ...."

If you are using Rails, why don't you use the Models for that task, instead of raw sql?

于 2012-04-29T19:58:21.560 回答