0

根据此处之前的问题,我设法创建了数据集,打印了列出的所有食谱,现在我试图从该列表中选择一个食谱并显示其标题、说明和成分。指令通过 pkID 列映射到食谱,成分通过 recipeID 列映射到食谱。当我在 Sqlite 数据库浏览器上打开数据库时,我可以在 Tables 下拉列表中访问此信息,因此我认为它们的正确名称是数据库中的表。

我无法通过 pkID 和 recipeID “过滤”,因此在选择一个食谱后,只显示适当的内容。

这是我在 Genie 中尝试做的 Python 代码:

  def PrintSingleRecipe(self,which):
    sql = 'SELECT * FROM Recipes WHERE pkID = %s' % str(which)
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    for x in cursor.execute(sql):
      recipeid =x[0]
      print "Title: " + x[1]
      print "Serves: " + x[2]
      print "Source: " + x[3]
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    sql = 'SELECT * FROM Ingredients WHERE RecipeID = %s' % recipeid
    print 'Ingredient List:'
    for x in cursor.execute(sql):
        print x[1]
    print ''
    print 'Instructions:'
    sql = 'SELECT * FROM Instructions WHERE RecipeID = %s' % recipeid
    for x in cursor.execute(sql):
      print x[1]
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    resp = raw_input('Press A Key -> ')

我无法改进我的大部分代码,似乎无法在这里使用我之前使用的在 step 语句中迭代的方法。这是我在 Genie 中的进展情况:

def PrintSingleRecipe(db:Database)
    stmt:Statement = PreparedStatements.select_all( db )
    res:int = UserInterface.raw_input("Select a recipe -> ").to_int()
    cols:int = stmt.column_count ()
    var row = new dict of string, string
    item:int = 1
    print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    while res == ROW
        for i:int = 0 to (cols - 1)
            row[ stmt.column_name( i ) ] = stmt.column_text( i )
        stdout.printf( "%-5s", item.to_string( "%03i" ))
        stdout.printf( "%-30s", row[ "Title" ])
        stdout.printf( "%-20s", row[ "Serves" ])
        stdout.printf( "%-30s\n", row[ "Source" ])
    print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    print "Ingredient list"
    print " "
    stdout.printf("%-5s", item.to_string( "%03i" ))
4

1 回答 1

0

我找到了解决问题的方法,也许可以优化。现在已经足够了。

另一个问题的答案非常有帮助。我使用的解决方案是使用 exec 函数并将回调指向 PrintSingleRecipe()。

必须进行一些调整才能将其用作回调,但我得到了我需要的东西。

这是调用函数的代码:

while true
    response:string = UserInterface.get_input_from_menu()
    if response == "1" // Show All Recipes
        PrintAllRecipes(db)
    else if response is "2" // Search for a recipe
        pass
    else if response is "3" //Show a Recipe
        res:string = UserInterface.raw_input("Select a recipe -> ")
        sql:string = "SELECT * FROM Recipes WHERE pkID = " + res
        db.exec(sql, PrintSingleRecipe, null)
    else if response is "4"//Delete a recipe
        pass
    else if response is "5" //Add a recipe
        pass
    else if response is "6" //Print a recipe
        pass
    else if response is "0" //Exit
        print "Goodbye"
        break
    else
        print "Unrecognized command. Try again."

这是 PrintSingleRecipe 的样子:

def PrintSingleRecipe(n_columns:int, values:array of string, column_names:array of string):int
    print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    for i:int = 0 to n_columns
        stdout.printf ("%s = %s\n", column_names[i], values[i])
    print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    print "Ingredient list"
    print " "
    return 0
于 2015-12-05T11:55:09.387 回答