samedi 17 octobre 2015

Printing the results from a select query with Genie

I have created the database in SQL lite and improved the little program to handle it (list, add, remove records). At this point I am trying to list the contents from the database using the prepared statement step() function. However, I can't iterate over the rows and columns on the database.

I suspect that the reason for that is that I am not handling the statement appropriately in this line:

stmt:Sqlite.Statement = null

If that is the case, how to pass the statement from the main (init) function to the children function?

This is the entire code so far:

// Trying to do a cookbook program
// raw_imput for Genie included, compile with valac --pkg sqlite3 cookbook.gs
[indent=4]
uses Sqlite

def raw_input (query:string = ""):string
    stdout.printf ("%s", query)
    return stdin.read_line ()


init
    db : Sqlite.Database? = null
    if (Sqlite.Database.open ("cookbook.db3", out db) != Sqlite.OK)
        stderr.printf ("Error: %d: %s \n", db.errcode (), db.errmsg ())
        Process.exit (-1)

    loop:bool = true
    while loop = true
        print "==================================================="
        print "                 RECIPE DATABASE  "
        print " 1 - Show All Recipes"
        print " 2 - Search for a recipe"
        print " 3 - Show a Recipe"
        print " 4 - Delete a recipe"
        print " 5 - Add a recipe"
        print " 6 - Print a recipe"
        print " 0 - Exit"
        print "==================================================="
        response:string = raw_input("Enter a selection -> ")
        if response == "1" // Show All Recipes
            PrintAllRecipes()
        else if response is "2" // Search for a recipe
            pass
        else if response is "3" //Show a Recipe
            pass
        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"
            Process.exit (-1)
        else
            print "Unrecognized command. Try again."


def PrintAllRecipes ()
    print "%-5s%-30s%-20s%-30s", "Item", "Name", "Serves", "Source"
    print "--------------------------------------------------------------------------------------"
    stmt:Sqlite.Statement = null
    param_position:int = stmt.bind_parameter_index ("$UID")
    //assert (param_position > 0)

    stmt.bind_int (param_position, 1)
    cols:int = stmt.column_count ()
    while stmt.step () == Sqlite.ROW
        for i:int = 0 to cols
            i++
            col_name:string = stmt.column_name (i)
            val:string = stmt.column_text (i) 
            type_id:int = stmt.column_type (i)
            stdout.printf ("column: %s\n", col_name)
            stdout.printf ("value: %s\n", val)
            stdout.printf ("type: %d\n", type_id)


/*    while stmt.step () == Sqlite.ROW
            col_item:string = stmt.column_name (1)
            col_name:string = stmt.column_name (2)
            col_serves:string = stmt.column_name (3)
            col_source:string = stmt.column_name (4)
            print "%-5s%-30s%-20s%-30s", col_item, col_name, col_serves, col_source */

Extra questions are:

  • Does the definitions of functions should come before or after init? I have noticed that they wouldn't be called if I left all of them after init. But by leaving raw_input in the beginning the error disappeared.

  • I was trying to define PrintAllRecipes() within a class, for didactic reasons. But I ended up making it "invisible" to the main routine.

Many thanks,

Aucun commentaire:

Enregistrer un commentaire