我從服務器的sqlite表中插入記錄。它像一個魅力和記錄成功插入。現在我想要的是,當用戶下次啓動應用程序時,它將再次插入重複記錄。如何只向SQlite插入記錄並忽略已存在的記錄?
我想只插入新記錄,如果可用並忽略已存在的記錄。
db.execSQL("Insert into myTable(ProjectNo,ProjectId,ThirdParty,SealingNumber,BookNumber,SupplyType,ConsAcNo,KnoNo,MeterSrMo,ConsName,ConsAddress1,ConsAddress2,ConsAddress3,TelephoneNo, ConsMobileNo,SanctionLoad, MeterMake, MeterType, PhaseType, IsCompleted, Contractor ")) values ('" + c.getString("PROJECT_NUMBER") + "'," + c.getString("PROJECT_ID") + ",'" + c.getString("THIRD_PARTY") + "'," + c.getString("SEALING_NUMBER") + "," + c.getString("BOOK_NUMBER") + "," + c.getString("SUPPLY_TYPE") + "," + c.getString("CONS_ACCOUNT_NO") + "," + c.getString("KNO_NUMBER") + ",'" + c.getString("MTR_SERIAL_NO") + "','" + c.getString("CONS_NAME") + "','" + c.getString("CONS_ADDRESS1") + "','" + c.getString("CONS_ADDRESS2") + "','" + c.getString("CONS_ADDRESS3") + "','" + c.getString("TELEPHONE_NO") + "','" + c.getString("CONS_MOBILE_NO") + "','" + c.getString("SANCTION_LOAD") + "','" + c.getString("METER_MAKE") + "','" + c.getString("METER_TYPE") + "','" + c.getString("METER_PHASE") + "','N', '"+ c.getString("CONTRACTOR1") +"')");
我試過下面的查詢但我失敗了。
db.execSQL("Insert into FieldUtilityData (ProjectNo,ProjectId,ThirdParty,SealingNumber,BookNumber,SupplyType,ConsAcNo,KnoNo,MeterSrMo,ConsName,ConsAddress1,ConsAddress2,ConsAddress3,TelephoneNo, ConsMobileNo,SanctionLoad, MeterMake, MeterType, PhaseType, IsCompleted, Contractor WHERE NOT EXISTS(SELECT ConsAcNo FROM FieldUtilityData WHERE ConsAcNo = " + c.getString("CONS_ACCOUNT_NO") + ")) values ('" + c.getString("PROJECT_NUMBER") + "'," + c.getString("PROJECT_ID") + ",'" + c.getString("THIRD_PARTY") + "'," + c.getString("SEALING_NUMBER") + "," + c.getString("BOOK_NUMBER") + "," + c.getString("SUPPLY_TYPE") + "," + c.getString("CONS_ACCOUNT_NO") + "," + c.getString("KNO_NUMBER") + ",'" + c.getString("MTR_SERIAL_NO") + "','" + c.getString("CONS_NAME") + "','" + c.getString("CONS_ADDRESS1") + "','" + c.getString("CONS_ADDRESS2") + "','" + c.getString("CONS_ADDRESS3") + "','" + c.getString("TELEPHONE_NO") + "','" + c.getString("CONS_MOBILE_NO") + "','" + c.getString("SANCTION_LOAD") + "','" + c.getString("METER_MAKE") + "','" + c.getString("METER_TYPE") + "','" + c.getString("METER_PHASE") + "','N', '"+ c.getString("CONTRACTOR1") +"')");
保持一個** PRIMARY KEY **或列有** ** UNIQUE約束。在重複記錄的情況下,它會拋出一個錯誤(_primary約束失敗..._)。 –