Python SQLite3 AUTOINCREMENT 错误
Python SQLite3 AUTOINCREMENT Error
我正在用 Python、SQLite3 和 Bottle 为我妈妈制作一个购物清单程序,
我正在制作一个 table 以将我所有的项目放入但 item_id
和 AUTOINCREMENT
列上它不起作用:
c.execute("""CREATE TABLE categories (
category_id INTEGER NOT NULL,
category_name TEXT PRIMARY KEY NOT NULL)""")
c.execute("""CREATE TABLE products (
item_name TEXT PRIMARY KEY NOT NULL,
item_category_id INTEGER NOT NULL)""")
c.execute("""CREATE TABLE shopping_products (
item_id INTEGER AUTOINCREMENT,
item_name TEXT PRIMARY KEY NOT NULL,
item_category_id INTEGER NOT NULL,
item_quantity INTEGER NOT NULL,
item_date INTEGER NOT NULL)""")
在 shopping_products
table 上 AUTOINCREMENT
不断返回此错误:
sqlite3.OperationalError: near "AUTOINCREMENT": syntax error
几点:
您确定需要AUTOINCREMENT
吗?这是 not normally required in SQLite:
The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. It is usually not needed.
In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID (except in WITHOUT ROWID tables) which is always a 64-bit signed integer.
On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an unused integer, usually one more than the largest ROWID currently in use. This is true regardless of whether or not the AUTOINCREMENT keyword is used.
If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID assignment algorithm to prevent the reuse of ROWIDs over the lifetime of the database. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.
只要您不担心重用先前删除的行中的 ROWID INTEGER PRIMARY KEY
应该没问题。
如果你想在 SQLite 中使用 AUTOINCREMENT
它必须在 INTEGER PRIMARY KEY
列上,例如
item_id INTEGER PRIMARY KEY AUTOINCREMENT
您的 shopping_products
table 在 item_name
上有一个 TEXT PRIMARY KEY
。
您不能有两个主键,所以如果您希望 item_id
有 AUTOINCREMENT
,您需要停止使用 item_name
作为主键。无论如何,项目名称将是一个奇怪的主键。
我正在用 Python、SQLite3 和 Bottle 为我妈妈制作一个购物清单程序,
我正在制作一个 table 以将我所有的项目放入但 item_id
和 AUTOINCREMENT
列上它不起作用:
c.execute("""CREATE TABLE categories (
category_id INTEGER NOT NULL,
category_name TEXT PRIMARY KEY NOT NULL)""")
c.execute("""CREATE TABLE products (
item_name TEXT PRIMARY KEY NOT NULL,
item_category_id INTEGER NOT NULL)""")
c.execute("""CREATE TABLE shopping_products (
item_id INTEGER AUTOINCREMENT,
item_name TEXT PRIMARY KEY NOT NULL,
item_category_id INTEGER NOT NULL,
item_quantity INTEGER NOT NULL,
item_date INTEGER NOT NULL)""")
在 shopping_products
table 上 AUTOINCREMENT
不断返回此错误:
sqlite3.OperationalError: near "AUTOINCREMENT": syntax error
几点:
您确定需要
AUTOINCREMENT
吗?这是 not normally required in SQLite:The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. It is usually not needed.
In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID (except in WITHOUT ROWID tables) which is always a 64-bit signed integer.
On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an unused integer, usually one more than the largest ROWID currently in use. This is true regardless of whether or not the AUTOINCREMENT keyword is used.
If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID assignment algorithm to prevent the reuse of ROWIDs over the lifetime of the database. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.
只要您不担心重用先前删除的行中的 ROWID
INTEGER PRIMARY KEY
应该没问题。如果你想在 SQLite 中使用
AUTOINCREMENT
它必须在INTEGER PRIMARY KEY
列上,例如item_id INTEGER PRIMARY KEY AUTOINCREMENT
您的
shopping_products
table 在item_name
上有一个TEXT PRIMARY KEY
。您不能有两个主键,所以如果您希望
item_id
有AUTOINCREMENT
,您需要停止使用item_name
作为主键。无论如何,项目名称将是一个奇怪的主键。