vendredi 18 décembre 2015

Creating a function that searches within a SQLite database using the genie programming language

I am trying to do a small program that searches a specific entry within a SQLite database using the Genie programing language. However I am stuck in a point where the prepared statement is not selecting the proper entry. I suppose there is something wrong about the syntax of the statement.

First, here is the search function:

def SearchForRecipe (db:Database)
    print "-------------------------------"
    print " Search in"
    print "-------------------------------"
    print " 1 - Recipe Name"
    print " 2 - Recipe Source"
    print " 3 - Ingredients"
    print " 4 - Exit"
    searchin:string = UserInterface.raw_input("Enter Search Type -> ")
    search:string = " "
    sql:string = " "
    response:string=" "
    if searchin != "4"
        if searchin == "1"
            search = "Recipe Name"
        else if searchin is "2"
            search = "Recipe Source"
        else if searchin is "3"
            search = "Ingredients"
        else
            print "An Error Occured"
            print "--------------------------------------------------------------------------------------"
            Process.exit (-1)

        parm:string = searchin
        response = UserInterface.raw_input ("Search for what in "+ search +" (blank to exit) -> ")

        if parm == "1" // Recipe Name
            stmt:Statement = PreparedStatements.select_recipe(db, response)
            var row = new dict of string, string
            cols:int = stmt.column_count ()

            print cols.to_string()

            for i:int = 0 to (cols - 1)
                row[ stmt.column_name( i ) ] = stmt.column_text( i )
            stdout.printf( "%-30s", row[ "name" ])
            stdout.printf( "%-20s", row[ "servings" ])
            stdout.printf( "%-30s\n", row[ "source" ])

The problem is happening in the if loop: if parm == "1" // Recipe Name and in the following line when I ask to print the number of columns of the selected entry it returns me zero. I added that line in order to try to troubleshoot what was going on, but it might not be the best approach to it.

The prepared statement is within a namespace, here is how it looks like:

namespace PreparedStatements
    def select_recipe (db:Database, res:string) : Statement
        statement:Statement
        db.prepare_v2( "SELECT pkid,name,source,servings FROM Recipes WHERE name like" + res, -1, out statement)
        return statement

I suppose there is something wrong with the SELECT statement in the select_recipe function, because it is not picking the proper entry in the database.

Question: How to select the entry properly? Should I use another approach, or a prepared statement is the proper way to do it?

Aucun commentaire:

Enregistrer un commentaire