How to print a sqlite table content with genie programming language

时间:2015-10-30 23:01:43

标签: sqlite genie

Based on previous questions here I managed to create the dataset, print all recipes listed and now I am trying to pick one of the recipes from that list and show its Title, Instructions and Ingredients. The instructions are mapped to the Recipes via the pkID column and the ingredients are mapped to the Recipes through a recipeID column. When I open the database on Sqlite Database Browser I can access this information inside the Tables dropdown list, so I suppose the proper name for them are tables within the database.

I am not being able to "filter" by pkID and by recipeID, so that after picking one recipe, only the appropriate content is shown.

This is the code in Python of what I am trying to do in Genie:

  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 -> ')

I have not been able to improve much of my code, it seems that using the approach I used before of iterating in a step statement cannot be used here. This is how far I got in 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" ))

1 个答案:

答案 0 :(得分:0)

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

来自另一个question的答案帮助极大。我使用的解决方案是使用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