Python中的SQLLite在UNIQUE Key上抛出操作错误

时间:2018-06-18 09:30:20

标签: python sqlite

运行此代码时出现操作错误:

db.execute("CREATE TABLE tLPs (LP VARCHAR(20), Dir VARCHAR(255), Mercosur TINYINT, H0 TINYINT, H1 TINYINT, H2 TINYINT, H3 TINYINT, H4 TINYINT, H5 TINYINT, H6 TINYINT, H7 TINYINT, H8 TINYINT, keyid INT AUTO_INCREMENT, UNIQUE KEY(LP), PRIMARY KEY(keyid))");

错误说:

sqlite3.OperationalError: near "KEY": syntax error

要“修复它”,我将其更改为:

db.execute("CREATE TABLE tLPs (LP VARCHAR(20), Dir VARCHAR(255), Mercosur TINYINT, H0 TINYINT, H1 TINYINT, H2 TINYINT, H3 TINYINT, H4 TINYINT, H5 TINYINT, H6 TINYINT, H7 TINYINT, H8 TINYINT, keyid INT AUTO_INCREMENT, PRIMARY KEY(keyid))");

基本上删除了UNIQUE KEY(LP)约束。那么我做错了什么?我以前用其他表做过这种事情并且没有任何问题。第一次使用sqlite和python,虽然......

1 个答案:

答案 0 :(得分:0)

AUTOINCREMENT被忽略,因为它仅适用于INTEGER PRIMARY KEY(列类型必须拼写为INTEGER,而不是INT或其他具有整数亲和力的类型。)

更惯用的DDL将是;

CREATE TABLE tLPs (
  LP TEXT,
  Dir TEXT UNIQUE,
  Mercosur INTEGER,
  H0 INTEGER,
  H1 INTEGER, 
  H2 INTEGER, 
  H3 INTEGER, 
  H4 INTEGER, 
  H5 INTEGER, 
  H6 INTEGER, 
  H7 INTEGER, 
  H8 INTEGER, 
  keyid INTEGER PRIMARY KEY AUTOINCREMENT
)