开发者

should I use "query" + "update/insert" or "insert/replace" in android sqlite?

开发者 https://www.devze.com 2023-04-06 07:08 出处:网络
I have a table like this db.execSQL(\"CREATE TABLE \" + TABLE_NAME + \" (\" + _ID + \" INTEGER PRIMARY 开发者_开发百科KEY,\"

I have a table like this

        db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
                + _ID + " INTEGER PRIMARY 开发者_开发百科KEY,"
                + URL + " TEXT UNIQUE,"
                + SomeInfo + " TEXT,"
                + OtherInfo + " INTEGER"
                + ");");

So there is only one record for each URL value. When the user visit a URL, I need neither insert a new row, or update an existing row if URL is presented I could think of 2 ways:

  1. SQLiteDatabase.query first and apply an update if there is one
  2. Always use SQLiteDatabase.replace, in case the UNIQUE constraint fail, sqlite will replace the record.

    Which approach is better? Are there other suggestions? Thanks


Since this seems to be a commom issue (which I faced recently too) I found this post which might be helpful.

0

精彩评论

暂无评论...
验证码 换一张
取 消