删除SQLite数据库

时间:2017-09-21 13:59:35

标签: android sqlite android-sqlite

因此,在我的应用程序中,您必须为房间中的每个房间添加电子设备,以便测量您花费的能量等等。

我想要的是当我点击按钮删除特定设备时,该设备将从存储它的数据库中删除,而这只是设备名称作为条目。

我有一个代码但是当我点击删除按钮时,应用程序有点崩溃并返回到主页活动。

有人可以帮忙吗?

那是按钮:

button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button2.setVisibility(View.GONE);
            tableLayout1.setVisibility(View.GONE);
            imageView1.setVisibility(View.GONE);
            textView12.setVisibility(View.GONE);

            DatabaseOperations DOP;
            String equipamento;
            equipamento = textView12.getText().toString();

            DOP = new DatabaseOperations(CTX);
            Cursor CR = DOP.getUserInfo(DOP, equipamento);
            CR.moveToFirst();
            DOP.deleteEquip(DOP, equipamento);


            Toast.makeText(getBaseContext(),"O equipamento foi removido com sucesso.", Toast.LENGTH_LONG).show();

        }
    });

这是班级:

public Cursor getUserInfo (DatabaseOperations DOP, String equipamento){
    SQLiteDatabase SQ = DOP.getReadableDatabase();
    String selection = TableData.TableInfo.EQUIPAMENTO +" LIKE ?";
    String coloumns[] = {TableData.TableInfo.POTENCIA, TableData.TableInfo.QUANT, TableData.TableInfo.HORAS, TableData.TableInfo.SIMULT};
    String args [] = {equipamento};
    Cursor CR = SQ.query(TableData.TableInfo.TABLE_NAME, coloumns, selection, args, null, null, null);
    return CR;
}

public void deleteEquip(DatabaseOperations DOP, String equipamento){
    String selection = TableData.TableInfo.EQUIPAMENTO;
    String args[] = {equipamento};
    SQLiteDatabase SQ = DOP.getWritableDatabase();
    SQ.delete(TableData.TableInfo.TABLE_NAME, selection, args);
}

1 个答案:

答案 0 :(得分:0)

@EngWatt此代码位于DBHelper类中,您需要更改rowid以反映要删除的项目的名称。我认为这是equipamento 我们希望您有一个MVP设计来实现这一切

    /* Delete a record from database table named "TABLE_INFO" */
/* based on the selected records id in Col_ID*/
public void deleteDBRow(String rowid){

    db = this.getWritableDatabase();
    db.delete(TABLE_INFO, Col_ID + " = ?", new String[] { rowid });
    db.close();
    return;
}