使用 Genie 列出 SQL 精简版数据库中的记录

Listing the records on a SQL lite database with Genie

现在我已经创建了 SQL 数据库 (, ),我想用它做点什么。我已经启动了一个小函数来处理数据集内容的打印,但是我无法弄清楚如何:

这是我试图模仿的 python 代码:

  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

这是我的进度:

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

Genie `print` 格式化

Genie 打印命令使用 printf 格式 - 请参阅 https://en.wikipedia.org/wiki/Printf_format_string 在您的示例中,第一行是:

print "%-5s%-30s%-20s%-30s", "Item", "Name", "Serves", "Source"

负号左对齐,则数字为宽度

打开 SQLite 数据库

要打开您使用的 SQLite 数据库:

Database.open( "database_name.sqlite", out database )

就像创建数据库文件一样。如果不存在,SQLite 将创建数据库文件。所以打开和创建是同一个命令。

Genie `pass` 声明

存根 if 语句:

if true
    pass

存根函数:

def stub_example()
    pass

pass 语句也可以扩展到 classes 和名称空间,但这尚未实现。因此,如果您进入破解 Genie 解析器的阶段,这可能是一项有用的任务。现在你必须添加一个虚拟 value/function 到 class:

class placeholder
    dummy:string = ""

避免 `null`,使用合理的默认值

给一个标识符赋值null意味着它没有值,标识符是一个空指针。尝试访问空指针(例如在算术表达式中)将导致程序崩溃。这导致 C.A.R.Hoare 将他的 null 发明称为他的 "billion-dollar mistake" - 请参阅 https://en.wikipedia.org/wiki/Null_pointer#History 以获取引用。 C 程序员倾向于大量使用 null,因此 Genie 维护 null 以与 C 接口兼容。 但是,最好使用合理的默认值。对于您的 raw_input 函数,如果没有传递任何提示,则可以使用空字符串或默认提示,例如“>”。这也避免了您的 null 检查,这是使用 null 的另一个缺点。您必须不断检查标识符是否为空。 stdin.readline 也等到输入字符串。即使当用户刚按下回车时它是一个空字符串,所以它永远不会 returns null。您的函数可以重写为:

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

获取 Select 查询的结果

Valadoc 有使用 exec() 或准备好的语句的示例。虽然他们在瓦拉

对于 exec(),您传入一个回调函数,该函数会针对结果中的每一行调用。回调函数将传递结果中的列数、作为文本的值数组和列名数组。参见 http://valadoc.org/#!api=sqlite3/Sqlite.Database.exec

对于准备好的语句,step() 函数 returns Sqlite.ROWS 直到结果中不再有行。所以你循环它并从准备好的语句中读取列。参见 http://valadoc.org/#!api=sqlite3/Sqlite.Statement

P.S。对于您的菜单,您可以使用逐字字符串