2013-05-14 53 views
0

我正在創建一個Android應用程序,我需要能夠從我的表中刪除記錄,該記錄由兩列構成Name和_id兩種類型的文本,其中_id是主鍵。但是,如果要刪除記錄的_id的值,則SQLite查詢不起作用。該應用程序運行時沒有任何問題,但它不會刪除所需的行。我確信這些記錄存在於數據庫中。我對Android應用程序和SQLite相當陌生。我讀了其他類似的帖子,但我無法找到我的代碼有什麼問題。請幫忙。SQLite Android:SQLite刪除命令無法正常工作

注意:我也嘗試db.execSQL()代替db.delete(),但它也沒有幫助。

這是我的Contact_Delete.java文件。

package com.tintin.prototype_2; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Contact_Delete extends Activity { 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    setContentView(R.layout.contact_delete); 

    TextView tv = (TextView) findViewById(R.id.deletenumtv); 
    final EditText et = (EditText) findViewById(R.id.deletenumet); 
    Button b = (Button) findViewById(R.id.Submitdelete); 

    b.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      DbTest db = new DbTest(Contact_Delete.this); 
      String numtobedelted = et.getText().toString(); 
      if(numtobedelted.compareTo("")==0)Toast.makeText(getApplicationContext(), "Invalid entry", Toast.LENGTH_SHORT).show(); 
      else{ 
       if(db.deletevalues(numtobedelted)>0)Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show(); 
       else Toast.makeText(getApplicationContext(), "Total Number of Contacts="+db.getNumberofContacts(), Toast.LENGTH_SHORT).show(); 
      } 
      et.setText(""); 
      db.close(); 
     } 
    }); 
} 
} 

這是我的DbTest.java文件

package com.tintin.prototype_2; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.DatabaseUtils; 
import android.database.sqlite.SQLiteConstraintException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DbTest extends SQLiteOpenHelper{ 

static final String dbName = "demoDB"; 
static final String contactTable = "Contacts"; 
static final String ID = "Index"; 
static final String Name = "Name"; 
static final String Number = "_id"; 
static int idx=1; 

public DbTest(Context context){ 
    super(context, dbName, null, 3); 
} 

public void onCreate(SQLiteDatabase db){ 
    db.execSQL("CREATE TABLE Contacts (Name TEXT , _id TEXT PRIMARY KEY);"); 
} 

public void onUpgrade(SQLiteDatabase db,int oldVersion, int newVersion){ 
    Log.v("Upgrade", "Upgrading database from version " + oldVersion 
      + " to " 
      + newVersion); 
    db.execSQL("DROP TABLE IF EXISTS Contacts"); 
    onCreate(db); 
} 

public boolean insertvalues(String x, String y){ 
    boolean ret=true; 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues cv = new ContentValues(); 
    Log.v("Value of x", x); 
    Log.v("Value of y", y); 
    cv.put(Name, "'"+x+"'"); 
    cv.put(Number, "'"+y+"'"); 
    try { 
     db.insertOrThrow(contactTable, null, cv); 
    } catch (SQLiteConstraintException e) { 
     Log.v("Exception","Number already in database"); 
     ret=false; 
     e.printStackTrace(); 
    } 
    Log.v("done", "done"); 
    db.close(); 
    return ret; 
} 

public int deletevalues(String num){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    int i = db.delete(contactTable, "_id = '"+num+"'", null); 
    db.close(); 
    Log.v("end", "Delete query ran"); 
    return i; 
} 

public Cursor getallContacts(){ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor mCursor = db.rawQuery("Select * from Contacts", null); 
    db.close(); 
    return mCursor; 
} 

public int getNumberofContacts(){ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    int numRows = (int) DatabaseUtils.queryNumEntries(db, "Contacts"); 
    Cursor c = db.rawQuery("select * from Contacts",null); 
    Log.v("Number of Records"," :: "+c.getCount()); 
    c.close(); 
    return numRows; 
} 

public Cursor getContact(String name){ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor ret = db.query(contactTable, null, "Name="+name, null, null, null, null); 
    if(ret!=null)ret.moveToFirst(); 
    db.close(); 
    return ret; 
} 

} 

回答

0

在助手類提及,如:

 public void deletedatabase(String search, String status,String col) { 
     Cursor c = null; 
     int id = 0; 
     boolean statu = false; 
     String[] columns = new String[] { "id", "name", "address", "department" }; 
     c = db.query(STUDENT_TABLE, columns, 
     "name=? OR address=? OR department=?", new String[] { search, 
     search, search }, null, null, "id DESC"); 
     if (c.moveToFirst()){ 
    do { 
     if (c.getColumnCount() == 4) { 
     String[] str = new String[3]; 
     str[0] = c.getString(1); 
     str[1] = c.getString(2); 
     str[2] = c.getString(3); 
     id = c.getInt(0); 
     statu = true; 
     } 
     } while (c.moveToNext()); 
    } 

     if (c != null && !c.isClosed()) { 
     c.close(); 
    } 
     if (statu) { 
     if (status.equals("update")) { 
    updateData(col, search,id); 
    } 
     if (status.equals("delete")) { 
     if (id != 0) { 
     deletedata(id); 
    } 
    } 
} 
} 

public void deletedata(int id) { 
db.delete(STUDENT_TABLE, "id=?", new String[] { String.valueOf(id) }); 
} 

然後在您的胡亞蓉:

 String searc=null; 
    if(!nameET.getText().toString().equals("")){ 
    searc=nameET.getText().toString(); 
    SQLHelper.deletedatabase(searc,"delete",""); 
    DATA=SQLHelper.selectalldatabase(); 
    lv.setAdapter(new MyCustomBaseAdapter(this, DATA)); 
    }else if(!addressET.getText().toString().equals("")){ 
    searc=addressET.getText().toString(); 
    SQLHelper.deletedatabase(searc,"delete",""); 
    DATA=SQLHelper.selectalldatabase(); 
    lv.setAdapter(new MyCustomBaseAdapter(this, DATA)); 
    } else if(!deptET.getText().toString().equals("")){ 
    searc=deptET.getText().toString(); 
    SQLHelper.deletedatabase(searc,"delete",""); 
    DATA=SQLHelper.selectalldatabase(); 
    lv.setAdapter(new MyCustomBaseAdapter(this, DATA)); 
    } else{ 
    Toast.makeText(this, "Please Enter Search keywork of any one Field", 10).show(); 
} 
+0

我完全不理解你的答案。我沒有你寫的這樣的專欄,我也不明白你在這裏做什麼。 – tintin 2013-05-14 07:35:06