简体   繁体   English

如何从 android 中的 json rss 提要解析“src”

[英]How to parse "src" from json rss feed in android

my SQLite database我的 SQLite 数据库

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.最简单的方法是在构成“已经存在”的一个或多个列上创建一个唯一索引并使用 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例如,假设 AGENCY_LINK 列相同是“已经存在”的条件,您可以将表创建为

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.插入时,您可以使用方便的SQLiteDatabase 插入方法,该方法使用INSERT OR IGNORE ,如果 AGENECY_LINK 值已经存在,则不会插入该行并返回 -1。

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.另一种方法,在不需要 UNQIUE 索引的情况下,可以使用 SELECT 查询检查是否存在可能插入的行,该查询具有 WHERE 子句,如果该行已经存在,则该行将 select 插入,然后仅在行不存在。

Example例子

The following is a working example that demonstrates the above.以下是演示上述内容的工作示例。

First the DatabaseHelper class which creates 2 tables:-首先创建 2 个表的DatabaseHelper class:-

  • the agency table that has the AGENCY_LINK column defined with the UNIQUE attribute, and具有使用 UNIQUE 属性定义的AGENCY_LINK列的代理表,以及
  • the alternative_agency table that has a composite UNIQUE index with 3 columns.具有 3 列的复合 UNIQUE 索引的Alternative_agency表。

The DatabaseHelper also has 3 methods defined for inserting individual rows:- DatabaseHelper 还定义了 3 种用于插入单个行的方法:-

  • 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. insertAgencyRow使用 SQliteDatabase插入方法(隐含 ISNERT OR IGNORE)插入到代理表中,因此如果AGENCY_LINK列的值已存在,则不会插入行。

  • 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. insertAlternativeAgencyRow插入(类似于 insertAgencerRow 方法)到具有复合索引的Alternative_agency表中,因此除非所有三个值都相同,否则将插入行。

  • the insertIfNotDuplicate queries the table with a where clause that would find only the row trying to be inserted. insertIfNotDuplicate使用 where 子句查询表,该子句将仅找到尝试插入的行。 If any rows are found then the row will not be inserted.如果找到任何行,则不会插入该行。

    class DatabseHelper extends SQLiteOpenHelper { class DatabseHelper 扩展 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 :-为了证明有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替代代理在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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