简体   繁体   English

Sqlite3 Python:不能使用“limit”作为列名

[英]Sqlite3 Python: can't use "limit" as column name

There are 6 columns and for some reason when my program gets to this bit of code during install, it simply creates a blank file with no table.有 6 列,出于某种原因,当我的程序在安装过程中遇到这段代码时,它只是创建一个没有表格的空白文件。

Through trial and error, I found the only thing that did not create a blank file was removing the limit row.通过反复试验,我发现唯一没有创建空白文件的是删除限制行。

I have other code that runs and looks the same just for different databases and it works fine.我还有其他代码,它们只针对不同的数据库运行并且看起来相同,并且运行良好。

        try:
            # Connect to Database
            conn = sqlite3.connect('databases/Categories.db')
            cur = conn.cursor()

            # Create Table
            cur.execute("""CREATE TABLE categories (
                priority text,
                name text,
                type text,
                increment text,
                total real,
                limit real)""")

            # Commit and Close
            conn.commit()
            conn.close()
        except sqlite3.OperationalError:
            pass

"limit" is an SQL keyword , for example, as in “limit”是一个 SQL关键字,例如,如

SELECT foo 
  FROM bar
  LIMIT 10;

If you want to use "limit" as a column name in sqlite, it needs to be quoted, in one of these ways:如果要在 sqlite 中使用“limit”作为列名,则需要使用以下方式之一将其引用:

  • 'limit' '限制'
  • "limit" “限制”
  • [limit] [限制]
  • `limit` `限制`

So for example, your statement could be因此,例如,您的陈述可能是

        cur.execute("""CREATE TABLE categories (
            priority text,
            name text,
            type text,
            increment text,
            total real,
            "limit" real)""")

Note that it must be quoted in other statements too, for example请注意,它也必须在其他语句中被引用,例如

"""INSERT INTO categories ("limit") VALUES (?);"""

I did some more testing and I fixed it by renaming the limit row to something else.我做了更多测试,并通过将限制行重命名为其他名称来修复它。 Turns out, sqlite3 doesn't like rows named limit.事实证明,sqlite3 不喜欢名为 limit 的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM