当前位置: 首页 > 面试题库 >

用Genie在Sqlite数据库中创建表?

单耘豪
2023-03-14
问题内容

我正在尝试使用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)

任何帮助将不胜感激。


问题答案:

似乎您已经在Valadoc中使用了该示例,但是没有将类型信息从Vala语法转换为Genie语法。所以Sqlite.Database dbdb: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)

需要注意的几件事:

  • 精灵只能返回成功的结果,因此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 :-)


 类似资料:
  • 问题内容: 我正在使用http://code.google.com/p/sqlite- jdbc/wiki/Introduction中 的SQLite驱动程序。 上述文档中显示的示例显示了如何连接现有数据库。 在我的应用程序中,我需要创建一个SQLite数据库。怎么做?创建具有扩展名的文件是否足够?还有一个叫做的函数。如果可以,如何使用?我用谷歌搜索,没有人给出明确的答案。 问题答案: 如果文件不

  • 主要内容:语法,实例,.dump 命令SQLite 的 sqlite3 命令被用来创建新的 SQLite 数据库。您不需要任何特殊的权限即可创建一个数据。 语法 sqlite3 命令的基本语法如下: 通常情况下,数据库名称在 RDBMS 内应该是唯一的。 另外我们也可以使用 .open 来建立新的数据库文件: 上面的命令创建了数据库文件 test.db,位于 sqlite3 命令同一目录下。 打开已存在数据库也是用 .open 命令,

  • 创建数据库之后,如何执行DDL语句来创建表?

  • 我是< code>sqlite数据库的新手,在我的项目中,我需要在创建数据库后添加数据。我尝试了这种方式,这是我的代码。 公共类主要活动扩展活动 { } 我创建了另一个类来打开sqlite数据库。 公共类MySqlHelper扩展SQLiteOpenHelper{ 公共字符串getBird(int id){ 它给我异常 帮助我避免这种情况,并在创建数据库后立即插入数据。

  • 问题内容: 我是Libgdx的新手,在使用游戏数据库时遇到麻烦。 我搜索了有关如何使用Libgdx使SQLite在Android和桌面应用程序上运行的教程,但没有找到一个简单的教程。 我上一次在Android中使用数据库时,创建了一个从扩展的类。 有没有一种简单的方法可以使用Libgdx做到这一点?或者至少,有人可以指出我的逐步教程或类似内容吗? 编辑 我忘了说我正在寻找可以让我管理诸如的版本的东

  • 我目前面临着将迁移与TypeORM和sqlite3数据库一起使用的问题。我希望在本地/测试/登台/生产环境之间尽可能多地使用奇偶校验,只在运行时使用将在docker compose文件中传递给docker的环境变量。我希望避免使用ormconfig。json文件和同步方法。 现在,在本地,我尝试使用一个dev配置文件来运行迁移,该文件似乎正在运行,但是迁移表不知何故是空的,并且从未创建过URL实体