gpt4 book ai didi

java - 在ANDROID SQLite中,是否可以在不将其设置为主键的情况下进行自动增量

转载 作者:行者123 更新时间:2023-11-30 04:14:59 25 4
gpt4 key购买 nike

我在 Android Sqlite 中创建表时尝试使用 INTEGER AUTOINCRMENT。但它显示错误。但在提供 INTEGER PRIMARY KEY AUTOINCRMENT 时它工作得很好。

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

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)");

最佳答案

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

 -- 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 来改进这一点,但您应该了解基本概念。

关于java - 在ANDROID SQLite中,是否可以在不将其设置为主键的情况下进行自动增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18609699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com