使用 Genie 列出 SQL lite 数据库上的记录 [英] Listing the records on a SQL lite database with Genie

查看:19
本文介绍了使用 Genie 列出 SQL lite 数据库上的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

  • 在打印命令上调整字符串之间的空格,以使其正确对齐?可以使用 ljust() 在 python 中做到这一点,但如何使用 Genie 做类似的事情?
  • 遍历数据集上的所有条目?据我所知,精灵中没有光标的等价物(不过,也许我在这里错了).
  • 连接到创建的数据库?如何打开它?
  • 加点:如何用精灵创建存根?我想创建空函数和类,为代码提供一个空结构,但是在精灵中没有 pass 命令,而且编译器似乎不接受空的 if 语句或函数.立>
  • 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.

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

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

这是我得到的程度:

[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` Formatting

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

Genie `print` Formatting

The Genie print command uses printf formatting - see https://en.wikipedia.org/wiki/Printf_format_string In your example the first line would be:

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

减号用于左对齐,然后数字是宽度.

The minus sign serves to left justify, then the number is the width.

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

To open an SQLite database you use:

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

就像您创建数据库文件一样.如果数据库文件不存在,SQLite 将创建该文件.所以 open 和 create 是同一个命令.

Just as you do to create the database file. SQLite will create the database file if it does not exist. So open and create are the same command.

存根 if 语句:

if true
    pass

存根函数:

def stub_example()
    pass

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

The pass statement could also be extended to classes and namespaces, but this has not been implemented. So if you get to the stage of hacking on the Genie parser it could be a useful task. For now you have to add a dummy value/function to the class:

class placeholder
    dummy:string = ""

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

给标识符赋值 null 意味着它没有值,标识符是一个空指针.尝试访问空指针(例如在算术表达式中)将导致程序崩溃.这导致 CARHoare 称他的 null 发明是他的十亿美元的错误"——参见 https://en.wikipedia.org/wiki/Null_pointer#History 引用.C 程序员倾向于大量使用 null,因此 Genie 维护 null 以与 C 接口兼容.但是,最好使用合理的默认值.对于您的 raw_input 函数,如果没有通过提示,则可以使用空字符串或默认提示,例如>".这也避免了空检查,这是使用空值的另一个缺点.您必须不断检查标识符是否为空.stdin.readline 也会等待输入字符串.即使当用户按下回车键时它是一个空字符串,它也永远不会返回 null.您的函数可以重写为:

Avoid `null`, Use Sensible Defaults

Assigning null to an identifier means it does not have a value, the identifier is a null pointer. Trying to access a null pointer, such as in arithmetic expressions, will cause the program to crash. This lead C.A.R.Hoare to call his invention of null his "billion-dollar mistake" - see https://en.wikipedia.org/wiki/Null_pointer#History for the quote. C programmers tend to use null a lot so Genie maintains null for compatibility with C interfaces. It is better, however, to use a sensible default value. For your raw_input function if no prompt is passed then an empty string is fine or a default prompt such "> ". This also avoids your null check, which is another disadvantage of using null. You have to constantly check an identifier is not null. stdin.readline also waits until a string is entered. Even if it is an empty string when the user just presses enter, so it never returns null. Your function could be re-written as:

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

获取选择查询的结果

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

Getting the Results of Select Query

Valadoc has examples for using either exec() or a prepared statement. Although they are in Vala.

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

For exec() you pass in a callback function that gets called for each row in the result. The callback function will be passed the number of columns in the result, an array of values as text and an array of column names. See http://valadoc.org/#!api=sqlite3/Sqlite.Database.exec

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

For a prepared statement the step() function returns Sqlite.ROWS until there are no more rows in the result. So you loop over that and read the columns from the prepared statement. See http://valadoc.org/#!api=sqlite3/Sqlite.Statement

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

P.S. For your menu you could have used a verbatim string

这篇关于使用 Genie 列出 SQL lite 数据库上的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆