在Android中逐行删除数据库中的记录

时间:2013-09-26 05:51:46

标签: android database

我正在尝试构建我的第一个Android应用程序,但我遇到了一个问题:我在数据库中编写了以下代码用于插入记录,但我不知道如何删除行,所以有人可以帮助我吗? ?? 插入记录代码:

public void btnAddEmp_Click(View view)
{
    boolean ok=true;
    try
    {
        Spannable spn=txtAge.getText();
        String name=txtName.getText().toString();
        int age=Integer.valueOf(spn.toString());
        int deptID=Integer.valueOf((int)spinDept.getSelectedItemId());
        Student emp=new Student(name,age,deptID);

        dbHelper.AddEmployee(emp);

    }
    catch(Exception ex)
    {
        ok=false;
        CatchError(ex.toString());
    }
    finally
    {
        if(ok)
        {
            //NotifyEmpAdded();
            Alerts.ShowEmpAddedAlert(this);
            txtEmps.setText("Number of students "+String.valueOf(dbHelper.getEmployeeCount()));
        }
    }
}

2 个答案:

答案 0 :(得分:1)

DELETE FROM tableName WHERE fieldName = value

这是删除查询。使用execSql

执行此操作

修改:使用execSql代替rawQuery

答案 1 :(得分:1)

您可以使用SQLiteDatabase.execSQL()执行此操作,并且不要尝试 rawQuery 用于查询。

SQLiteDatabase db = this.getWritableDatabase();

db.execSQL("DELETE FROM tableName where fieldName=Value"); 

db.close();

查看详细答案here