我正在尝试遍历从我的数据库中提取的食谱,解析每个配方中的成分并将成分插入到自己的表中。 的问题是,我不知道如何将我的内心选择当前成分变量: 目前,我发现了以下错误: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