在Android中,从数据库行中删除记录

时间:2022-11-19 01:53:45

I'm trying to build my first Android application, but I'm experiencing a problem: I write following code for insert record in database, but I don't know how to delete a row, so can anybody help me??? Code for insert record:

我正在尝试构建我的第一个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 个解决方案

#1


1  

DELETE FROM tableName WHERE fieldName = value

This is delete query. Execute it with execSql

这是删除查询。与execSql执行它

Edit: use execSql instead of rawQuery

编辑:使用execSql代替rawQuery

#2


1  

You can simply do this using SQLiteDatabase.execSQL() and don't try rawQuery It's meant for querying.

您可以使用sqlitedatabas . execsql()来实现这一点,并且不要尝试使用用于查询的rawQuery。

SQLiteDatabase db = this.getWritableDatabase();

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

db.close();

Check out the detailed answer here

请在这里查看详细的答案。

#1


1  

DELETE FROM tableName WHERE fieldName = value

This is delete query. Execute it with execSql

这是删除查询。与execSql执行它

Edit: use execSql instead of rawQuery

编辑:使用execSql代替rawQuery

#2


1  

You can simply do this using SQLiteDatabase.execSQL() and don't try rawQuery It's meant for querying.

您可以使用sqlitedatabas . execsql()来实现这一点,并且不要尝试使用用于查询的rawQuery。

SQLiteDatabase db = this.getWritableDatabase();

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

db.close();

Check out the detailed answer here

请在这里查看详细的答案。