vendredi 16 octobre 2015

Listing the records on a SQL lite database with Genie

Now that I have created the SQL database (1, 2), I want to do something with it. I have started a small function to handle the printing of the dataset contents, however I am unable to figure out how to:

  • adjust space between strings on a print command, in order to it become properly justified? One can do it in python using ljust(), but how to make something similar with Genie?
  • iterate across all entries on the dataset? As far as I understand there is no equivalent of a cursor in Genie (maybe I am wrong here, though).
  • connect to the created database? How to open it?
  • Extra point: how to create a stub with genie? I wanted to create empty functions and classes,to give an empty structure to the code, however there is no pass command in genie and it seems that the compiler do not accept empty if statements or functions.

This is the python code I am trying to mimic:

  def PrintAllRecipes(self):
    print '%s %s %s %s' % ('Item'.ljust(5),'Name'.ljust(30),'Serves'.ljust(20),'Source'.ljust(30))
    print '--------------------------------------------------------------------------------------'
    sql = 'SELECT * FROM Recipes'
    cntr = 0
    for x in cursor.execute(sql):
      cntr += 1
      print '%s %s %s %s' % (str(x[0]).rjust(5),x[1].ljust(30),x[2].ljust(20),x[3].ljust(30))
      print '--------------------------------------------------------------------------------------'
      self.totalcount = cntr

This is how far I have got:

[indent=4]

class Cookbook
    def PrintAllRecipes()
        print "%s %s %s %s" % ("Item".ljust(5),"Name".ljust(30),"Serves".ljust(20),"Source".ljust(30))
        print "--------------------------------------------------------------------------------------"
        var sql = "SELECT * FROM Recipes"
        var cntr = 0
            for x in cursor.execute(sql)
                cntr += 1
                print "%s %s %s %s" % (str(x[0]).rjust(5),x[1].ljust(30),x[2].ljust(20),x[3].ljust(30))
                print "--------------------------------------------------------------------------------------"
                self.totalcount = cntr


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

init
    //def Menu()
        //cbk = Cookbook()
    //var 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 -> ")

Aucun commentaire:

Enregistrer un commentaire