我正在用Python、SQLite3和list为我的妈妈制作购物清单程序,我正在做一张桌子把我所有的物品都放进去,但是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表上,AUTOINCREMENT继续返回此错误:
sqlite3.OperationalError: near "AUTOINCREMENT": syntax error发布于 2018-08-25 19:20:37
以下几点:
AUTOINCREMENT吗?是在SQLite中通常不需要1. 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.
2. 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.
3. 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.
4. 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就应该是可以的。
AUTOINCREMENT与SQLite一起使用,则它必须位于INTEGER PRIMARY KEY列上。
item_id整数主键自动生成shopping_products表在item_name上有一个TEXT PRIMARY KEY。
您不能有两个主键,所以如果您想让item_id拥有AUTOINCREMENT,就需要停止使用item_name作为您的主键。项目名称无论如何都是一个奇怪的主键。https://stackoverflow.com/questions/52020450
复制相似问题