Delete first N rows in android sqlite database

后端 未结 7 1937
北恋
北恋 2020-12-14 09:37

Please let me know how to delete n-rows in android sqlite database. I used this code:

   String ALTER_TBL =\"delete from \" + MYDATABASE_TABLE +
         \"w         


        
相关标签:
7条回答
  • 2020-12-14 10:36

    It is a bit long procedure but you can do it like this

    first get the ids column of table from which which you want to delete certain values

    public Cursor KEY_IDS() {
        Cursor mCursor = db.rawQuery("SELECT KEYID " +
                                         " FROM MYDATABASE_TABLE ;", null);
    
    
                if (mCursor != null)
                {
                mCursor.moveToFirst();
                }
    
                return mCursor;
    }
    

    Collect it in an array list

    ArrayList<String> first = new ArrayList<String>();
    
    cursor1 = db.KEY_IDS();
                cursor1.moveToFirst();
                startManagingCursor(cursor1);
    
                for (int i = 0; i < cursor1.getCount(); i++) {
    
                    reciv1 = cursor1.getString(cursor1
                            .getColumnIndex(DBManager.Player_Name));
    
    second.add(reciv1);
    
    }
    

    and the fire delete query

    for(int i = 0 ;i<second.size(); i++)
    {
    db.delete(MYDATABASE_TABLE KEYID +"=" + second.get(i) , null);
    
    }
    
    0 讨论(0)
提交回复
热议问题