我在Android Sqlite中创建Table时尝试使用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)");
答案 0 :(得分: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
来改进这一点,但您应该了解基本想法。
答案 1 :(得分:0)
在SQLite中,AUTOINCREMENT
列must be declared as INTEGER PRIMARY KEY。
答案 2 :(得分:0)
是的,实现这一点非常困难,唯一的选择是将其作为INTEGER PRIMARY KEY AUTOINCREMENT ......