【问题标题】:Listing the records on a SQL lite database with Genie使用 Genie 列出 SQLite 数据库中的记录
【发布时间】:2015-10-16 10:17:11
【问题描述】:

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

  • 调整打印命令中字符串之间的间距,以使其正确对齐?使用 ljust() 在 python 中可以做到这一点,但是如何用 Genie 做类似的事情呢?
  • 遍历数据集上的所有条目?据我了解,在 Genie 中没有相当于光标的功能(但也许我在这里错了)。
  • 连接到创建的数据库?如何打开?
  • 额外点:如何使用精灵创建存根?我想创建空函数和类,为代码提供一个空结构,但是 genie 中没有 pass 命令,编译器似乎不接受空 if 语句或函数。

这是我试图模仿的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 -> ")

【问题讨论】:

    标签: python sqlite genie


    【解决方案1】:

    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 将创建它。所以 open 和 create 是同一个命令。

    精灵`pass`声明

    一个存根if 声明:

    if true
        pass
    

    一个存根函数:

    def stub_example()
        pass
    

    pass 语句也可以扩展到类和命名空间,但这还没有实现。因此,如果您进入了破解 Genie 解析器的阶段,这可能是一项有用的任务。现在你必须在类中添加一个虚拟值/函数:

    class placeholder
        dummy:string = ""
    

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

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

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

    获取选择查询的结果

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

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

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

    附:对于您的菜单,您可以使用逐字字符串

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多