代码之家  ›  专栏  ›  技术社区  ›  Gray droiddeveloper

derby和hsqldb中的表名和字段名转义问题

  •  4
  • Gray droiddeveloper  · 技术社区  · 16 年前

    我有点问题 ORMLite package . 当我为一个表生成模式时,我认为转义所有实体名是一个好的实践。这将保护一些Java类或字段名不为SQL保留字:

    CREATE TABLE "footable" ("stuff" VARCHAR(255))
    

    I'm now adding "raw" query support so that ORMLite can help users perform their own queries. However, I find that with Derby and Hsqldb, the entity names cannot be used 没有 逃逸。例如,以下查询:

    SELECT * FROM footable
    

    generates the following errors:

    Derby: ERROR 42X05: Table/View 'FOOTABLE' does not exist.
    Hsqldb: Table not found in statement [select * from footable]
    

    如果select表也被转义为 "footable" . The other databases supported by ORMLite work fine with or without the escaping: MySQL, Postgres, Microsoft SQL Server, H2, and Sqlite.

    Are there better ways to escape reserved words in Derby and Hsqldb? Other ideas about how to do this in a portable manner?

    谢谢。

    2 回复  |  直到 14 年前
        1
  •  3
  •   Gray droiddeveloper    14 年前

    所以,尽管布莱恩的回答不太正确,但他还是对我表示了敬意。

    结果是因为我正在创建数据库 "footable" 然后,正如布莱恩所说,这将是创造案例的敏感。但是,当我在 footable ( 没有 引用)Derby和Hsqldb都在推广它为大写,所以我实际上在做:

    SELECT * FROM FOOTABLE
    

    It's not about being case insensitive without the quotes (which would have worked) but about promoting the entity names to be all capitals when there are no quotes and then matching by case. I'd argue there was a bug here...

    无论如何,我已经更改了我的Derby和hsqldb,使中的所有实体名称都大写。 ORMLite 一切都在运转。丑陋的伊莫,但工作。

        2
  •  2
  •   Bryan Pendleton    16 年前

    你只需要确保箱子是匹配的。

    所以如果是:

    create table "Footable" ("Stuff" varchar (25))

    然后必须是:

    insert into "Footable" ("Stuff") values 'hi mom'

    如果表/列名称使用双引号,则大小写将保持原样。

    如果表/列名称不在双引号中,那么Derby处理它时不区分大小写。