Search code examples
androidsqliteandroid-arrayadapter

How to create Comment, but if the same exists, delete the old one, in sqlite database?


I have comment in listView "www.google.lv".
If I am trying to put again the same comment("www.google.lv"), it should create this comment, but the same previous comment should be delete from database. They should not be duplicated, but is created as the lastest comment.

Is it possible?


Solution

  • how to createComment, but if the same exists, it delete the old one, in sqlite database?

    Simply use the command INSERT OR REPLACE ....

    • If the entry doesn't exist, it will be inserted into the database.
    • If it does exist, the old entry will be replaced with the new entry.

    Addition
    Since you are using ContentValues to insert data into your database. Call database.insertWithOnConflict(), instead of database.insert(). Try:

    database.insertWithOnConflict(MySQLiteHelper.TABLE_COMMENTS, null, values, SQLiteDatabase.CONFLICT_REPLACE);
    

    (This assumes that you have an appropriate UNIQUE constraint on your database...)