如何仅向SQlite插入记录并忽略已存在的记录?

时间:2016-09-16 05:46:47

标签: android json database sqlite android-studio

我在服务器的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") +"')");

3 个答案:

答案 0 :(得分:1)

检查使用where条件,如果匹配而不是更新表,或者插入。

这样做,

console.log(`<Apple is red>
<Grapes - Purple>
<Strawberries are Red>`.replace(/>\s/g, '>'))

答案 1 :(得分:0)

  public int getFullCount(String table, String where) {
        Cursor cursor = db.query(false, table, null, where, null, null, null, null, null);
        try {
            if (cursor != null) {
                cursor.moveToFirst();
                no = cursor.getCount();
                cursor.close();
            }
        } finally {
            cursor.close();
        }
        return no;
    }

使用此功能并传递表名称&amp;条件。在插入支票计数之前,如果计数返回值大于0,则不要添加记录,否则添加记录

答案 2 :(得分:0)

ConAcNo 设置为表的主键,并使用SQLiteDatabase类的insert(...)方法。请记住,它不会抛出任何错误,并在出现任何错误时返回-1。如果您尝试插入重复记录,则会导致错误并返回您可以忽略的-1

Documentation of insert() method

Guide for Saving Data in SQL Databases