简体   繁体   中英

How to parse "src" from json rss feed in android

my SQLite database

How to identify if data already exists in my table or not? if yes then don't add new row data else add new row data.

The simplest way is to have a UNIQUE index on the column or columns that constitutes "already exists" and use INSERT OR IGNORE.

eg assuming that the AGENCY_LINK column being the same is an "already exists" condition you could have the table created as

CREATE TABLE IF NOT EXISTS the_table (ID INTEGER PRIMARY KEY, AGENCY_NAME TEXT, AGENCY_CATEGORY TEXT, AGENCY_LINK TEXT UNIQUE);

When inserting you could use the convenience SQLiteDatabase insert method which uses INSERT OR IGNORE , If the AGENECY_LINK value already existed then the row would not be inserted and -1 would be returned.

An alternative, where no UNQIUE index would be required, could be to check for the existence of the row to potentially be inserted with a SELECT query that has a WHERE clause that would select the row if it already exists and to then only insert if the row does not exist.

Example

The following is a working example that demonstrates the above.

First the DatabaseHelper class which creates 2 tables:-

  • the agency table that has the AGENCY_LINK column defined with the UNIQUE attribute, and
  • the alternative_agency table that has a composite UNIQUE index with 3 columns.

The DatabaseHelper also has 3 methods defined for inserting individual rows:-

  • the insertAgencyRow uses the SQliteDatabase insert method (implied ISNERT OR IGNORE) to insert into the agency table, thus a row will not be inserted if the AGENCY_LINK column has a value that already exists.

  • the insertAlternativeAgencyRow inserts (similar to the insertAgencerRow method) into the alternative_agency table that has the composite index, thus rows will be inserted unless all three values are the same.

  • the insertIfNotDuplicate queries the table with a where clause that would find only the row trying to be inserted. If any rows are found then the row will not be inserted.

    class DatabseHelper extends SQLiteOpenHelper {

     public static final String DATABASE_NAME = "the_database"; public static final int DATABASE_VERSION = 1; public static final String TABLE_NAME_AGENCY = "agency"; public static final String COLUMN_ID = "ID"; public static final String COLUMN_AGENCY_NAME = "AGENCY_NAME"; public static final String COLUMN_AGENCY_CATEGORY = "AGENCY_CATEGORY"; public static final String COLUMN_AGENCY_LINK = "AGENCY_LINK"; public static final String TABLE_NAME_ALTERNATIVE_AGENCY = "alternative_agency"; SQLiteDatabase db; private DatabseHelper(Context context) { super(context,DATABASE_NAME,null,DATABASE_VERSION); db = this.getWritableDatabase(); } private volatile static DatabseHelper instance = null; public static DatabseHelper getINSTANCE(Context context) { if (instance == null) { instance = new DatabseHelper(context); } return instance; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_AGENCY +"(" + COLUMN_ID + " INTEGER PRIMARY KEY" + "," + COLUMN_AGENCY_NAME + " TEXT" + "," + COLUMN_AGENCY_CATEGORY + " TEXT" + "," + COLUMN_AGENCY_LINK + " TEXT UNIQUE" + ")"); /* Alternative table using a composite unique index accross multiple columns */ db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_ALTERNATIVE_AGENCY +"(" + COLUMN_ID + " INTEGER PRIMARY KEY" + "," + COLUMN_AGENCY_NAME + " TEXT" + "," + COLUMN_AGENCY_CATEGORY + " TEXT" + "," + COLUMN_AGENCY_LINK + " TEXT " + ")"); /* The COMPOSITE UNIQUE INDEX (all three columns have to be the same for a UNIQUE conflict )*/ db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_"+ TABLE_NAME_ALTERNATIVE_AGENCY + "_allcolumns ON " + TABLE_NAME_ALTERNATIVE_AGENCY + "(" + COLUMN_AGENCY_NAME + ","+COLUMN_AGENCY_CATEGORY + ","+COLUMN_AGENCY_LINK + ")"); } @Override public void onUpgrade(SQLiteDatabase db, int i, int i1) { } public long insertAgencyRow(String agencyName, String agencyCategory, String agencyLink) { ContentValues cv = new ContentValues(); cv.put(COLUMN_AGENCY_NAME,agencyName); cv.put(COLUMN_AGENCY_CATEGORY,agencyCategory); cv.put(COLUMN_AGENCY_LINK,agencyLink); return db.insert(TABLE_NAME_AGENCY,null,cv); } public long insertAlternativeAgencyRow(String agencyName, String agencyCategory, String agencyLink) { ContentValues cv = new ContentValues(); cv.put(COLUMN_AGENCY_NAME,agencyName); cv.put(COLUMN_AGENCY_CATEGORY,agencyCategory); cv.put(COLUMN_AGENCY_LINK,agencyLink); return db.insert(TABLE_NAME_ALTERNATIVE_AGENCY,null,cv); } public long insertIfNotDuplicate(String agencyName, String agencyCategory, String agencyLink) { long rv = -1; String whereClause = COLUMN_AGENCY_NAME + "=?" + " AND " + COLUMN_AGENCY_CATEGORY + "=?" + " AND " + COLUMN_AGENCY_LINK + "=?"; Cursor csr = db.query(TABLE_NAME_AGENCY,null,whereClause,new String[]{agencyName,agencyCategory,agencyLink},null,null,null); int rowcount = csr.getCount(); csr.close(); if (rowcount == 0) { ContentValues cv = new ContentValues(); cv.put(COLUMN_AGENCY_NAME,agencyName); cv.put(COLUMN_AGENCY_CATEGORY,agencyCategory); cv.put(COLUMN_AGENCY_LINK,agencyLink); return db.insert(TABLE_NAME_AGENCY,null,cv); } return rv; }

    }

To demonstrate there is MainActivity :-

public class MainActivity extends AppCompatActivity {
    DatabseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dbHelper = DatabseHelper.getINSTANCE(this);

        /* Inserting into the Agency table where the AGENCY_LINK column is UNIQUE */
        dbHelper.insertAgencyRow("Agency1","Category1","Agency1_Link_001");
        dbHelper.insertAgencyRow("Agency2","Category2","Agency2_Link_001");
        dbHelper.insertAgencyRow("Agency3","Category3","Agency3_Link_001");
        dbHelper.insertAgencyRow("Agency3","Category3","Agency3_Link_001"); //<<<<< duplicate
        dbHelper.insertAgencyRow("Agency3A","Category3A","Agency3_Link_001"); //<<<<< duplicate as LINK the same

        /* Inserting into the Alternative Agency tale where all 3 columns constitute a duplicate */
        dbHelper.insertAlternativeAgencyRow("Agency1","Category1","Agency1_Link_001");
        dbHelper.insertAlternativeAgencyRow("Agency2","Category2","Agency2_Link_001");
        dbHelper.insertAlternativeAgencyRow("Agency3","Category3","Agency3_Link_001");
        dbHelper.insertAlternativeAgencyRow("Agency3","Category3","Agency3_Link_001"); //<<<<< duplicate
        dbHelper.insertAlternativeAgencyRow("Agency3A","Category3A","Agency3_Link_001"); //<<<<< NOT duplicate

        /* Inserting into Agency table using the SELECT */
        dbHelper.insertIfNotDuplicate("Agency4","Category4","Agency4_Link_001");
        dbHelper.insertIfNotDuplicate("Agency4","Category4","Agency4_Link_002"); /* Link different so inserted */
        dbHelper.insertIfNotDuplicate("Agency4","Category4","Agency4_Link_002"); /*<<<<< all columns duplicated not inserted */
        dbHelper.insertIfNotDuplicate("Agency4A","Category4","Agency4_Link_001A"); /* name different so inserted */
        dbHelper.insertIfNotDuplicate("Agency4","Category4A","Agency4_Link_001B"); /* category different so inserted */
    }
}

When the App is run (for the first time) then the database has the two tables, without the duplicate rows. The following are the tables as per App Inspection :-

The agency table在此处输入图像描述

The alternativeAgency table在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM