1

我在 Android Sqlite 中创建表时尝试过使用 INTEGER AUTOINCREMENT。但它显示错误。但它在给 INTEGER PRIMARY KEY AUTOINCREMENT 时工作正常。

我的要求是,带有 AUTOINCREMENT 的列不应该是主键。

db.execSQL("CREATE TABLE " + Table_KOT_ITEMS_TEMP + "(" + col_Item_Name
                + " varchar(40) NOT NULL," + col_Ref_No
                + " int(11) NOT NULL DEFAULT '0'," + col_Table_No
                + " varchar(6) NOT NULL DEFAULT '0'," + col_Kot_No
                + " int(11) NOT NULL DEFAULT '0'," + col_Main_Item_Code
                + " varchar(10) NOT NULL," + col_Item_Type
                + " varchar(10) NOT NULL," + col_Item_Amount
                + " decimal(10,2) DEFAULT '0.00'," + col_Item_Qty
                + " decimal(9,3) NOT NULL DEFAULT '0.000'," + col_No_Of_Persons
                + " varchar(6) NOT NULL," + col_Dum_Unique
                + " varchar(25) NOT NULL," + col_CheckBox
                + " char(5) NOT NULL," + col_Item_Auto_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT)");
4

3 回答 3

2

不是直接的。但是您可以创建一个触发器来执行此操作。

 -- Create table
 CREATE TABLE testtbl (
     id INTEGER PRIMARY KEY,
     autoincr DEFAULT NULL,
     otherrow TEXT -- and maybe more
 );
 -- Create helper table
 CREATE TABLE sequence_helper (
     name TEXT PRIMARY KEY COLLATE NOCASE,
     seq INTEGER DEFAULT 1
 );
 -- Fill in the starting number
 INSERT INTO sequence_helper VALUES ('testtbl', 1);
 -- Create the trigger
 CREATE TRIGGER autoincr_testtbl AFTER INSERT ON testtbl FOR EACH ROW BEGIN
     UPDATE testtbl SET 
         autoincr = (SELECT seq FROM sequence_helper WHERE name = 'testtbl')
         WHERE rowid = new.rowid;
     UPDATE sequence_helper SET seq = seq+1 WHERE name = 'testtbl';
 END;

您可以通过检查 autoincr 列是否为 来改进这一点NULL,但您应该了解基本概念。

于 2013-09-04T11:54:29.383 回答
0

是的,这很难实现,唯一的选择是把它作为整数主键自动增量......

于 2013-09-06T04:55:27.880 回答
0

在 SQLite 中,必须将AUTOINCREMENT声明为 INTEGER PRIMARY KEY

于 2013-09-04T09:30:56.663 回答