首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python SQLite3自动测试错误

Python SQLite3自动测试错误
EN

Stack Overflow用户
提问于 2018-08-25 19:10:20
回答 1查看 1.5K关注 0票数 1

我正在用Python、SQLite3和list为我的妈妈制作购物清单程序,我正在做一张桌子把我所有的物品都放进去,但是item_idAUTOINCREMENT放在列里,它不起作用:

代码语言:javascript
复制
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继续返回此错误:

代码语言:javascript
复制
sqlite3.OperationalError: near "AUTOINCREMENT": syntax error
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-25 19:20:37

以下几点:

  1. 你确定需要AUTOINCREMENT吗?是在SQLite中通常不需要
代码语言:javascript
复制
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就应该是可以的。

  1. 如果您确实希望将AUTOINCREMENT与SQLite一起使用,则它必须位于INTEGER PRIMARY KEY列上。 item_id整数主键自动生成
  2. 您的shopping_products表在item_name上有一个TEXT PRIMARY KEY。 您不能有两个主键,所以如果您想让item_id拥有AUTOINCREMENT,就需要停止使用item_name作为您的主键。项目名称无论如何都是一个奇怪的主键。
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52020450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档