我正在嘗試遍歷從我的數據庫中提取的食譜,解析每個配方中的成分並將成分插入到自己的表中。 的問題是,我不知道如何將我的內心選擇當前成分變量: 目前,我發現了以下錯誤:ruby on rails:如何在select語句中包含局部變量
ExtractIngredients.rb:21:在'查詢':未知列'ing'in'field list'(Mysql :: Error)
請問如何在我的select語句中包含「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
你幾乎從不想直接從Rails查詢數據庫。你爲什麼需要這樣做? – Gareth