Android删除查询数据库

时间:2014-12-30 13:24:11

标签: android sql sqlite

我试图删除我的应用程序中的查询数据库,但我有一点问题。 所以,在删除代码中我有4个案例:

  1. 数据库为空 - > MESSAGE TOAST DATABASE EMPTY
  2. txtfield empty - >消息TOAST TXTFIELD EMPTY
  3. 在DB中找不到txtfield中的单词 - > MESSAGE TOAST字号未找到
  4. 已删除确定 - > DELETE QUERY + TOAST NOTIFICATION
  5. 我的问题是3°情况,我使用外部方法来验证db中是否存在单词! 这是代码:

    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();
        }
    }
    

1 个答案:

答案 0 :(得分:3)

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

应该是:

return find;

的Intead
return false;