Creating a table in a Sqlite database with Genie?

后端 未结 1 2044
[愿得一人]
[愿得一人] 2020-12-22 01:51

I am trying to create a database using Genie code. However, I am facing problems with documentation, so I am asking here!

This can be considered unintuitive, because

相关标签:
1条回答
  • 2020-12-22 01:58

    It looks as though you have used the example in Valadoc, but not converted the type information from Vala syntax to Genie syntax. So Sqlite.Database db would be db:Sqlite.Database.

    A working example in Genie would be:

    [indent=4]
    init
        db:Sqlite.Database   
        errmsg:string
    
        ec:int = Sqlite.Database.open("cookbook.sqlite", out db)
        if ec != Sqlite.OK
            stderr.printf("Can't open database: %d: %s\n", db.errcode(), db.errmsg())
            Process.exit( -1 )
    
        query:string ="""CREATE TABLE Recipes (
            pkiD INTEGER PRIMARY KEY, 
            name TEXT,
            servings TEXT, 
            source TEXT 
            )
        """
        db.exec (query, null, out errmsg)
    

    A couple of things to note:

    • Genie can only return a successful outcome, so return -1 isn't currently allowed. This may change at some point, see https://bugzilla.gnome.org/show_bug.cgi?id=707233 To work around this you can use GLib's Process.exit(), as used in the example above. This has the disadvantage that the program terminates immediately without object destruction. So if you are using final in your classes to close the database connection, for example, the final block will not be called. Or you can just return, which always returns 0
    • Verbatim strings, """I'm a verbatim string""", are great for embedding SQL in Genie :-)
    0 讨论(0)
提交回复
热议问题