I am making a shopping list program for my mum with Python, SQLite3 and Bottle, I am making a table to put all my items into but the item_id
with AUTOINCREMENT
on the column it doesn't work:
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)""")
On the shopping_products
table the AUTOINCREMENT
keep returning this error:
sqlite3.OperationalError: near "AUTOINCREMENT": syntax error
A few points:
Are you sure you need AUTOINCREMENT
? It's 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.
As long as you're not worried about reusing ROWIDs from previously deleted rows INTEGER PRIMARY KEY
should be fine.
If you do want to use AUTOINCREMENT
with SQLite it must be on an INTEGER PRIMARY KEY
column, eg
item_id INTEGER PRIMARY KEY AUTOINCREMENT
Your shopping_products
table has a TEXT PRIMARY KEY
on item_name
.
You can't have two primary keys, so if you want item_id
to have AUTOINCREMENT
you'll need to stop using item_name
as your primary key. An item name would be a strange primary key anyway.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.