Я делаю программу списка покупок для моей мамы с Python, SQLite3 и Bottle,
Я делаю таблицу для размещения всех своих предметов, но 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_productsAUTOINCREMENT продолжает возвращать эту ошибку:
sqlite3.OperationalError: near "AUTOINCREMENT": syntax error






Несколько моментов:
Вы уверены, что вам нужен AUTOINCREMENT? Это обычно не требуется в 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 подойдет.
Если вы действительно хотите использовать AUTOINCREMENT с SQLite, он должен быть в столбце INTEGER PRIMARY KEY, например.
item_id INTEGER PRIMARY KEY AUTOINCREMENT
В вашем столе shopping_products есть TEXT PRIMARY KEY на item_name.
У вас не может быть двух первичных ключей, поэтому, если вы хотите, чтобы у item_id был AUTOINCREMENT, вам нужно прекратить использовать item_name в качестве первичного ключа. В любом случае имя элемента будет странным первичным ключом.
Для этого конкретного случая, я думаю, это из того же документа лучше ответит: Because AUTOINCREMENT keyword changes the behavior of the ROWID selection algorithm, AUTOINCREMENT is not allowed on WITHOUT ROWID tables or on any table column other than INTEGER PRIMARY KEY. Any attempt to use AUTOINCREMENT on a WITHOUT ROWID table or on a column other than the INTEGER PRIMARY KEY column results in an error.