0

ここでの以前の質問に基づいて、データセットを作成し、リストされているすべてのレシピを印刷して、そのリストからレシピの 1 つを選択し、そのタイトル、説明書、および材料を表示しようとしています。指示は pkID 列を介してレシピにマップされ、材料はレシピ ID 列を介してレシピにマップされます。Sqlite Database Browser でデータベースを開くと、Tables ドロップダウン リスト内のこの情報にアクセスできるので、それらの適切な名前はデータベース内のテーブルであると思います。

1 つのレシピを選択した後、適切なコンテンツのみが表示されるように、pkID とレシピ ID で「フィルタリング」することはできません。

これは、私が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 -> ')

私は自分のコードの多くを改善することができませんでした。以前に使用したステップ ステートメントでの反復のアプローチを使用することは、ここでは使用できないようです。これは私が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 に答える