Android删除查询数据库

标签 android sql sqlite

我试图在我的应用程序中删除查询数据库,但我遇到了一个小问题。 所以,在删除代码中我有 4 种情况:

  1. 数据库为空 -> MESSAGE TOAST DATABASE EMPTY
  2. txtfield 为空 -> MESSAGE TOAST TXTFIELD EMPTY
  3. 在数据库中找不到 txtfield 中的单词 -> MESSAGE TOAST 找不到单词
  4. 删除 OK -> DELETE QUERY + TOAST NOTIFICATION

我的问题是3°案例,我使用外部方法验证数据库中是否存在单词! 这是代码:

public boolean trovato(String n){

    boolean find = false;
    int k=0;

    Cursor c=db.rawQuery("SELECT * from TABLE_NAME", null);
    int count= c.getCount();
    c.moveToFirst();

    String[] nomi = new String[count];


    while(c.moveToNext())
    {
       nomi[k]= c.getString(c.getColumnIndex("fname"));
       if(nomi[k].equalsIgnoreCase(n)){
           find = true;
           break;
       }else{
           k++;
       }


    }
    return false;
}

public void delete(View data){

    EditText delete=(EditText)findViewById(R.id.txtdel);
    String delete2 = delete.toString();
    Cursor c=db.rawQuery("SELECT * from TABLE_NAME", null);
    int count= c.getCount();   

    if(count==0){
        Toast.makeText(getApplicationContext(),"DATABASE VUOTO",Toast.LENGTH_LONG).show();
    }

    else if(delete.getText().toString().trim().length()==0){
        Toast.makeText(getApplicationContext(),"STRINGA VUOTA, RIPROVA", Toast.LENGTH_LONG).show();

    }else if(trovato(delete.toString())){

        Toast.makeText(getApplicationContext(),"NON ESISTE !!!!",Toast.LENGTH_LONG).show();
    }else{
        db.execSQL("DELETE FROM TABLE_NAME WHERE fname='"+delete.getText()+"'");
        Toast.makeText(getApplicationContext(),"DELETE OK",Toast.LENGTH_LONG).show();
    }
}

最佳答案

在函数 trovato(String n) 中,您总是返回 false。

应该是:

return find;

代替

return false;

关于Android删除查询数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27706317/

相关文章:

android - 在 Android Market 中发布 App

android - android 现在可以在通话中处理文本到语音吗?

java - 当我的应用程序在 Android 10/Q 上关闭时,如何触发启动 Activity Intent ?

php - 如何将php数组保存到mysql表中?

MySQL/MariaDB 删除表(如果创建)

android - IllegalArgumentException : Navigation action/destination cannot be found from the current destination Destination [Navigation Error]

mysql - 如何对重复记录进行分组,然后更新除每个组的最低 ID 以外的所有成员?

android - 你如何根据 SQLite 文档调整 Android 的 SQLite,例如杂注缓存大小?

python - SQLITE3 Python 更新带有变量的记录不起作用?

ios - 我是否需要做任何额外的工作来保护我的 iOS 应用程序中的 sqlite 数据库?