2015-10-30 80 views
0

根據之前的問題,我設法將create數據集,print列出的所有食譜,現在我試圖從該列表中選擇其中一個食譜並顯示其標題,說明和成分。說明通過pkID列映射到配方,並通過recipeID列將配料映射到配方。當我在Sqlite數據庫瀏覽器上打開數據庫時,我可以在Tables下拉列表中訪問這些信息,所以我想他們的專有名稱是數據庫中的表格。如何使用genie編程語言打印sqlite表格內容

我無法通過pkID和recipeID進行「過濾」,因此在選取一個配方後,只顯示適當的內容。

這是什麼我想在精靈做的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 -> ') 

我一直沒能改善很多我的代碼,似乎是用我迭代之前使用的方法在一個步驟聲明中不能在這裏使用。這是我得到多遠的精靈:

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

回答

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