简体   繁体   English

Android SQLite:在删除行之前如何知道数据是否存在

[英]Android SQLite: How to know data exist before deleting row

Trying to deleting a row inside a table using query by id. 尝试使用ID查询删除表内的一行。 Suppose that id not exists, then how to know that id or row info not exists? 假设ID不存在,那么如何知道ID或行信息不存在?

code below: 下面的代码:

public void deletePerson(String personId){
        SQLiteDatabase sqldb = getWritableDatabase();
        sqldb.execSQL("DELETE FROM " + TABLE_NAME + " WHERE " + COLUMN_NAME + " = '" + personId+"'");
        Toast.makeText(context,"Record removedId", Toast.LENGTH_SHORT).show();
    }

You don't necessarily need to know, DELETE won't fail if there is no record that meets the where criteria. 您不一定需要知道,如果没有符合where条件的记录,则DELETE不会失败。

However, if you need to know if the row(s) where deleted, then if you use the delete convenience method it returns the number of affected rows. 但是,如果您需要知道行是否已删除,则如果使用delete便捷方法,它将返回受影响的行数。 If 0 then no rows were deleted. 如果为0,则不删除任何行。

As such the code could be :- 因此,代码可能是:-

public void deletePerson(String personId){
    SQLiteDatabase sqldb = getWritableDatabase();
    if (sqldb.delete(TABLE_NAME,COLUMN_NAME + "=?",new String[]{personId}) > 0) {
        Toast.makeText(context,"Record removedId", Toast.LENGTH_SHORT).show();
    }
}
  • The delete convenience methods generates from the minimal set of parameters passed, the encoding including enclosing the value in single quotes, so it reduces the chance of syntax errors. 删除便捷方法从传递的最小参数集生成,编码包括将值括在单引号中,因此它减少了语法错误的可能性。
  • Additionally, it offers protection against SQL injection attacks. 此外,它提供了针对SQL注入攻击的保护。
  • The 3rd parameter, being an array can have multiple parameters, each matches a ? 作为数组的第三个参数可以具有多个参数,每个参数都与一个?匹配? (which is replaced by the value) according to the sequence. (由值替换),具体取决于顺序。
    • ie the first ? 即第一个? is replaced by the value from the first element, the second ? 被第一个元素的值替换,第二个元素? is replaced by the value from 2nd element and so on. 被第二个元素的值替换,依此类推。
    • ?'s and number of elements must match. ?和元素数必须匹配。
public boolean getPerson(String personId){
    SQLiteDatabase sqldb = getWritableDatabase();
    Cursor c=     sqldb.execSQL("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_NAME + " = '" + personId+"'");
    if(c!=null){
        return true;
    }
    return false;
}

You can create a function which returns boolean value based on existence of data in sqlite. 您可以创建一个基于sqlite中数据的存在返回boolean value的函数。

public boolean isExists(String personId)
{
    SQLiteDatabase sqldb = getWritableDatabase();
    String selectString = "SELECT * FROM " + _TABLE + " WHERE " + _ID + " =?";
    Cursor cursor = sqldb.rawQuery(selectString, new String[] {personId}); 

    boolean isExist = false;

    if(cursor.moveToFirst()){
       isExist = true;
    }

    sqldb.close();              
    return isExist;
}

Referenced from Android SQLite Database CRUD . Android SQLite数据库CRUD引用。

Okay, after getting all of your answers, I solved my problem and what I did is: 好吧,在得到所有答案之后,我解决了我的问题,我所做的是:

public void deletePerson(String personId){
        SQLiteDatabase sqldb = getWritableDatabase();
        Cursor cursor = sqldb.rawQuery("SELECT " + COLUMN_NAME + " FROM " + TABLE_NAME + " WHERE " + COLUMN_NAME + " = '" + personId + "'", null);
        if(cursor.getCount()!=0){
            sqldb.execSQL("DELETE FROM " + TABLE_NAME + " WHERE " + COLUMN_NAME + " = '" + personId + "'");
            Toast.makeText(context,"Successfully deleted", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(context,personId+" not found", Toast.LENGTH_LONG).show();
        }
    }

Thanks ! 谢谢 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM