我正在开发结算安卓应用。只有当表格中的所有行重复时,我才需要防止重复。
我添加了回滚方法
rows = db.insertWithOnConflict(“output_to_cloud4”,null,cv,SQLiteDatabase.CONFLICT_REPLACE);
但仍然插入重复的详细信息
- 我正在分享我的插入方法
void Insert_OutputDetails_withTransation() {
long rows = 0;
db = openOrCreateDatabase("Student.db",OPEN_READWRITE, null);
ContentValues cv = new ContentValues();
db.beginTransaction();
try {
for (int i = 0; i < myMap.size(); i++) {
float check_not_zeroValue = Float.parseFloat(myMap.get(PosUtils.PRODUCT_CODE.get(i)));
if (!(check_not_zeroValue == 0.0f)) {
try {
int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String ts = tsTemp.toString();
Double round_amount = Double.valueOf(Rounded_Total_price_with_tax) - Double.valueOf(Total_price_with_tax);
cv.put("BILLNO", (LAST_BILL_NUMBER));
cv.put("BILLPREFIX", SALES_MAN_BILLPREFIX);
cv.put("BILLDATE", Date);
cv.put("saleman_code", SALES_MAN_CODE);
cv.put("DLYVEHICLE", SALES_MAN_DLYVANNO);
//"CUSTOMERCODE","PRODUCTCODE","QTY","RATE","GROSS",
cv.put("CUSTOMERCODE", CUSTOMERS_CODE_LIST.get(CUSTOMER_POSITION));
cv.put("PRODUCTCODE", PRODUCT_CODE.get(i));
cv.put("QTY", myMap.get(PosUtils.PRODUCT_CODE.get(i)));
cv.put("RATE", PRODUCT_RATE.get(i));
cv.put("GROSS", new DecimalFormat("0.00").format(item_price));
//"CGST","CGSTAMT","SGSTP","SGSTAMT","CESSP",
cv.put("CGST", PRODUCT_CGSTP.get(i));
cv.put("CGSTAMT", new DecimalFormat("0.00").format(amount_cgst));
cv.put("SGSTP", PRODUCT_SGSTP.get(i));
cv.put("SGSTAMT", new DecimalFormat("0.00").format(amount_sgst));
cv.put("CESSP", PRODUCT_CESSP.get(i));
//"CESSAMT","SCESSP","SCCAMT","ROUND","BTOTAL","
cv.put("CESSAMT", new DecimalFormat("0.00").format(amount_cseep));
cv.put("SCESSP", PRODUCT_SCESSP.get(i));
cv.put("SCCAMT", new DecimalFormat("0.00").format(amount_scessp));
cv.put("BTOTAL", new DecimalFormat("0.00").format(Rounded_Total_price_with_tax));
cv.put("ROUND", new DecimalFormat("0.00").format(round_amount));
cv.put("BILL_STATUS", "1");
rows = db.insertWithOnConflict("output_to_cloud4", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
} catch (Exception in) {
in.getMessage();
}
}
}
db.setTransactionSuccessful();
} catch (Exception e) {
e.getMessage();
} finally {
db.endTransaction();
}
}
我的数据库Formate
db.execSQL("CREATE TABLE IF NOT EXISTS output_to_cloud3(" +
"BILLNO TEXT,BILLPREFIX TEXT,BILLDATE TEXT,saleman_code TEXT,DLYVEHICLE TEXT," +
"CUSTOMERCODE TEXT,PRODUCTCODE TEXT,QTY TEXT,RATE TEXT,GROSS TEXT," +
"CGST TEXT,CGSTAMT TEXT,SGSTP TEXT,SGSTAMT TEXT,CESSP TEXT," +
"CESSAMT TEXT,SCESSP TEXT,SCCAMT TEXT,ROUND TEXT,BTOTAL TEXT,BILL_STATUS TEXT,TIME_IN TEXT)");
如果有人在帮助我之前遇到这个问题.....
答案 0 :(得分:1)
我相信您的问题是您希望自动拒绝重复项。
情况并非如此,你必须定义CONSTRAINTS,除非暗示它们(例如主索引暗示了UNIQUE约束),这些都不是。
最简单的解决方案,虽然可能不是最有效的,但要满足的标准,我需要防止重复,只有当表中的所有行再次重复时。将创建一个基于所有的索引具有UNIQUE约束的行,因此您可以使用: -
CREATE UNIQUE INDEX IF NOT EXISTS stop_dupplicates ON output_to_cloud4 (
BILLNO,
BILLPREFIX,
BILLDATE,
saleman_code,
DLYVEHICLE,
CUSTOMERCODE,
PRODUCTCODE,
QTY,
RATE,
GROSS,
CGST,
CGSTAMT,
SGSTP,
SGSTAMT,
CESSP,
CESSAMT,
SCESSP,
SCCAMT,
ROUND,
BTOTAL,
BILL_STATUS,
TIME_IN
);
您可以使用等同于insertWithOnConflict
的{{1}}方法,而不是将SQLiteDatabase.CONFLICT_REPLACE
方法与insert
一起使用,因此如果遇到重复,则INSERT是只是忽略(未插入)并继续处理,而不是用完全相同的数据替换(更新)一行。
基于列的子集,为方便起见,以下内容演示了以上内容: -
INSERT OR IGNORE .......
CREATE TABLE IF NOT EXISTS output_to_cloud3(
BILLNO TEXT,
BILLPREFIX TEXT,
BILLDATE TEXT,
TIME_IN TEXT
);
CREATE UNIQUE INDEX IF NOT EXISTS stop_dupplicates ON output_to_cloud3 (
BILLNO,
BILLPREFIX,
BILLDATE,
TIME_IN
);
现在的表格是: -
INSERT INTO output_to_cloud3
VALUES
('A','B','C','D'),
('E','F','G','H'),
('I','J','K','L'),
('M','N','O','P'),
('Q','R','S','T')
;
方法): -
insertOrThrow
尝试第一次插入时将抛出异常,并且不会插入任何内容。
INSERT INTO output_to_cloud3
VALUES
('A','B','C','D'),
('E','F','G','H'),
('I','J','K','L'),
('M','N','O','P'),
('Q','R','S','T')
;
和insertWithOnConflict
。: -
SQLiteDatabase.CONFLICT_REPLACE
然后不会引发任何异常,也不会添加任何行,但是所有行都会更新,但表实际上没有变化。
INSERT OR REPLACE INTO output_to_cloud3
VALUES
('A','B','C','D'),
('E','F','G','H'),
('I','J','K','L'),
('M','N','O','P'),
('Q','R','S','T')
;
方法。: -
insert
然后不会引发任何异常,不会添加任何行,表格不变。
INSERT OR IGNORE INTO output_to_cloud3
VALUES
('A','B','C','D'),
('E','F','G','H'),
('I','J','K','L'),
('M','N','O','P'),
('Q','R','S','T')
;
方法。: -
insertOrThrow
然后抛出一个异常(由于它是单个语句,表将保持不变。)如果是单个插入,那么第一行将被插入,第二行将导致异常。
INSERT INTO output_to_cloud3
VALUES
('U','V','W','X'), -- New non-duplicate row
('A','B','C','D'),
('E','F','G','H'),
('I','J','K','L'),
('M','N','O','P'),
('Z','AA','AB','AC'), -- New non-duplicate row
('Q','R','S','T'),
('AD','AE','AF','AG') -- New non-dupliate row
;
方法和insertWithOnConflict
。: -
SQLiteDatabase.CONFLICT_REPLACE
这将插入非重复项并替换重复项(实际上保持不变),结果表如下所示。
INSERT OR REPLACE INTO output_to_cloud3
VALUES
('U','V','W','X'), -- New non-duplicate row
('A','B','C','D'),
('E','F','G','H'),
('I','J','K','L'),
('M','N','O','P'),
('Z','AA','AB','AC'), -- New non-duplicate row
('Q','R','S','T'),
('AD','AE','AF','AG') -- New non-dupliate row
;
方法。: -
insert
最终结果与尝试5相同,即: -