如何在SQLite中按ID删除?

时间:2017-03-30 06:46:30

标签: java android sqlite android-database

我是一个非常新的Android开发人员,并认为我应该首先尝试使用SQLite创建一个简单的ToDo List App。我具备所有基本功能:添加,更新和删除任务。但是,我按任务标题添加,更新和删除,而不是ID。这会导致重复任务出现问题(例如同时删除同名任务)。经过大量的互联网搜索,我仍然无法找到办法。我将不胜感激任何帮助!

这是我的代码:

public class TaskDbHelper extends SQLiteOpenHelper {

    public TaskDbHelper(Context context) {
        super(context, TaskContract.DB_NAME, null, TaskContract.DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String createTable = "CREATE TABLE " + TaskContract.TaskEntry.TABLE + " ( " +
                TaskContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                TaskContract.TaskEntry.COL_TASK_TITLE + " TEXT NOT NULL, " +
                TaskContract.TaskEntry.COL_TASK_DATE + " DATE);";
        sqLiteDatabase.execSQL(createTable);
    }
}

显示任务的活动

public class ShowTaskActivity extends AppCompatActivity {

    private TaskDbHelper mHelper;
    private ListView mTaskListView;
    private ArrayAdapter<String> mAdapter;

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);

        mHelper = new TaskDbHelper(this);
        mTaskListView = (ListView) findViewById(R.id.list_todo);

        updateUI();
    }

    private void updateUI() {
        ArrayList<String> taskList = new ArrayList<>();
        SQLiteDatabase sqLiteDatabase = mHelper.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.query(
                TaskContract.TaskEntry.TABLE,  // Name of the table to be queried
                new String[]{  // Which columns are returned
                        TaskContract.TaskEntry._ID,
                        TaskContract.TaskEntry.COL_TASK_TITLE,
                        TaskContract.TaskEntry.COL_TASK_DATE},
                null, null, null, null, null);
        while (cursor.moveToNext()) {
            int index =     cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
            taskList.add(cursor.getString(index));
        }

        if (mAdapter == null) {
            mAdapter = new ArrayAdapter<>(this,
                    task, // What view to use for the items
                    R.id.task_title, // Where to put the string of data
                    taskList); // Where to get the data
            mTaskListView.setAdapter(mAdapter);
        } else {
            mAdapter.clear();
            mAdapter.addAll(taskList);
            mAdapter.notifyDataSetChanged();
        }

        cursor.close();
        sqLiteDatabase.close();
    }

    // TODO: Change to delete by ID, not name
    public void deleteTask(View view) {
        View parent = (View) view.getParent();
        TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
        String task = taskTextView.getText().toString();
        SQLiteDatabase sqLiteDatabase = mHelper.getWritableDatabase();
        sqLiteDatabase.delete(
            TaskContract.TaskEntry.TABLE,  // Where to delete
            TaskContract.TaskEntry.COL_TASK_TITLE + " = ?",  // Boolean check
            new String[]{task});  // What to delete
        sqLiteDatabase.close();
        updateUI();
    }
}

任务添加代码

public void addTask(String task, String date) {
    SQLiteDatabase sqLiteDatabase = mHelper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
    contentValues.put(TaskContract.TaskEntry.COL_TASK_DATE, date);
    sqLiteDatabase.insertWithOnConflict(
            TaskContract.TaskEntry.TABLE,
            null,
            contentValues,
            SQLiteDatabase.CONFLICT_REPLACE);
    sqLiteDatabase.close();
}

3 个答案:

答案 0 :(得分:3)

String rowId; //Set your row id here
SQLiteDatabase sqLiteDatabase = mHelper.getWritableDatabase();
sqLiteDatabase.delete(
                TaskContract.TaskEntry.TABLE,  // Where to delete
                KEY_ID+" = ?",  
                new String[]{rowId});  // What to delete
sqLiteDatabase.close();

答案 1 :(得分:2)

SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_ID + " = ?",new String[]{Long.toString(id)} );
db.close();

答案 2 :(得分:0)

您可以尝试使用此方法删除按ID

  public void deleteData(String tableName, Integer id) {

    try {
        if (mWritableDB != null) {
            mWritableDB.execSQL("delete from " + tableName + " Where id = " + id);
        }
    } catch (Exception _exception) {
        _exception.printStackTrace();
    }
}