【问题标题】:Creating a table in a Sqlite database with Genie?使用 Genie 在 Sqlite 数据库中创建表?
【发布时间】:2015-10-09 06:40:06
【问题描述】:

我正在尝试使用 Genie 代码创建数据库。但是,我在文档方面遇到问题,所以我在这里问!

这可能被认为是不直观的,因为我可以直接在命令行上运行 sqlite 并创建数据集。我这样做是出于教育的原因。

我试图在 python 中模仿的代码是:

#--------------------------------------
import apsw
#--------------------------------------
# Opening/creating database. Database name is cookbook.db3
connection=apsw.Connection("cookbook.db3")
cursor=connection.cursor()
#--------------------------------------
# Create The Tables
#--------------------------------------
sql = 'CREATE TABLE Recipes (pkiD INTEGER PRIMARY KEY, name TEXT, servings TEXT, source TEXT)'
cursor.execute(sql)
sql = 'CREATE TABLE Instructions (pkID INTEGER PRIMARY KEY, instructions TEXT, recipeID NUMERIC)'
cursor.execute(sql)
sql = 'CREATE TABLE Ingredients (pkID INTEGER PRIMARY KEY, ingredients TEXT, recipeID NUMERIC)'
cursor.execute(sql)
#--------------------------------------
# Insert Data into tables
#--------------------------------------
# Insert data into Recipe table
sql = 'INSERT INTO Recipes (name,servings,source) VALUES ("Spanish Rice",4,"Greg")'
cursor.execute(sql)
# Get the pkid for the inserted record
sql = "SELECT last_insert_rowid()"
cursor.execute(sql)
for x in cursor.execute(sql):
    lastid = x[0]
# Insert data into the instructions table
sql = 'INSERT INTO Instructions (recipeID,instructions) VALUES( %s,"Brown hamburger. Stir in all other ingredients. Bring to a boil. Stir. Lower to simmer. Cover and cook for 20 minutes or until all liquid is absorbed.")' % lastid
cursor.execute(sql)
# Insert data into the ingredients table
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"1 cup parboiled Rice (uncooked)")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"1 pound Hamburger")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"2 cups Water")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"1 8 oz can Tomato Sauce")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"1 small Onion chopped")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"1 clove Garlic chopped")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"1 tablespoon Ground Cumin")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"1 teaspoon Ground Oregano")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"Salt and Pepper to taste")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"Salsa to taste")' % lastid
cursor.execute(sql)
# Put in one more for good "measure"
sql = 'INSERT INTO Recipes (name,servings,source) VALUES ("Pickled Pepper-Onion Relish","9 half pints","Complete Guide to Home Canning")'
cursor.execute(sql)
# Get the pkid for the inserted record
sql = "SELECT last_insert_rowid()"
cursor.execute(sql)
for x in cursor.execute(sql):
    lastid = x[0]
sql = 'INSERT INTO Instructions (recipeID,instructions) VALUES( %s,"Wash and chop vegetables. Combine all ingredients and boil gently until mixture thickens and volume is reduced by 1/2 (about 30 minutes). Fill sterile jars with hot relish, leaving 1/2 inch head space and seal tightly. Store in refrigerator and use within one month or process in boiling water bath if extended storage is desired. Hot pack process time at 0-1000 feet for 5 minutes, 1,001 to 6000 ft 10 minutes, above 6,000 ft 15 minutes.")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"6 cups finely chopped Onions")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"3 cups finely chopped Red Peppers")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"3 cups finely chopped Green Peppers")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"1 1/2 cups sugar")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"6 cups White Vinegar (5 percent)")' % lastid
cursor.execute(sql)
sql = 'INSERT INTO Ingredients (recipeID,ingredients) VALUES ( %s,"2 tablespoons canning or pickling salt")' % lastid
cursor.execute(sql)
# Tell us we are done
print 'Done'

我已经学会了如何使用vala.doc,并且我找到了操作sqlite数据库的例程:Sqlite.Database。但是,我在编译时不断收到错误消息。

这是我在复制该代码方面取得的进展:

/* HOW TO CREATE THE DB WITH GENIE */

// Opening/creating db. 

[indent=4]
init
    Sqlite.Database db
    string errmsg

    int ec = Sqlite.Database.open("cookbook.db", out db)
    if ec != Sqlite.OK
        stderr.printf("Can't open database: %d: Ss\n", db.errcode(), db.errmesg)
        return -1

    // Insert data
    query:string ="""
        CREATE TABLE Recipes (pkiD INTEGER PRIMARY KEY, name TEXT, servings TEXT, source TEXT)
        """
    db.exec (query, null, out errmsg)

本应将表插入数据库,但出现以下错误:

    valac --pkg sqlite3 cookcreate.gs 
cookcreate.gs:9.11-9.11: error: syntax error, expected `:' but got `.' with previous identifier
    Sqlite.Database db
          ^
Compilation failed: 1 error(s), 0 warning(s)

任何帮助将不胜感激。

【问题讨论】:

  • 你的代码看起来不像 Python..你在哪里编码?
  • 你是说第一个?它是python,它正在工作。在elementaryOS上编码。您已经注意到我正在尝试将其翻译成 Genie 编程语言,对吧?它不是 SQL 精灵。
  • Ohhh ryt...我的小姐。让我看看能不能帮上忙。

标签: python database sqlite genie


【解决方案1】:

看起来您已经使用了 Valadoc 中的示例,但没有将类型信息从 Vala 语法转换为 Genie 语法。所以Sqlite.Database db 将是db:Sqlite.Database

Genie 中的一个工作示例是:

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

有几点需要注意:

  • Genie 只能返回成功的结果,因此目前不允许使用 return -1。这可能会在某些时候发生变化,请参阅https://bugzilla.gnome.org/show_bug.cgi?id=707233 要解决此问题,您可以使用 GLib 的 Process.exit(),如上例中使用的那样。这样做的缺点是程序立即终止而不破坏对象。因此,如果您在类中使用final 来关闭数据库连接,例如,final 块将不会被调用。或者您可以只使用return,它总是返回 0
  • 逐字字符串 """I'm a verbatim string""" 非常适合在 Genie 中嵌入 SQL :-)

【讨论】:

  • 谢谢,这让我继续前进。但我最终提出了两个新问题:(i)逐字字符串是否需要引号之间的任何特定标识?
  • 不需要特定的缩进。逐字字符串中写入的内容被传递给 SQLite,其格式与写入的格式完全相同。只要 SQLite 可以处理它就可以了。为了便于阅读,我在示例中使用了类似的缩进。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-29
  • 2013-02-23
  • 2012-09-15
  • 2013-04-27
  • 2020-12-21
  • 1970-01-01
相关资源
最近更新 更多