2013-07-02 26 views
0

我正在構建一個簡單的應用程序,它存儲一些聯繫人並在android手機設備中檢索聯繫人。表名在Android設備的SQLite數據庫爲空

我已經創建了我自己的數據庫和一個表,並將值插入到手機中的表中。

我的手機沒有植根。所以我無法訪問這些文件,但是我發現這些值存儲在表中。並在仿真器上進行測試。直到這裏,它是好的。

通過從表中提取數據來顯示列表中的所有聯繫人。這也很好。

但問題是當我試圖刪除記錄時,它顯示錶名在logcat中爲空(不是例外),並且數據不會被刪除。但在模擬器中,數據從表中刪除。我無法通過電話達到此目的。

這是我刪除代碼,

public boolean onContextItemSelected(MenuItem item) { 

    super.onContextItemSelected(item); 
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item 
      .getMenuInfo(); 
    int menuItemIndex = item.getItemId(); 
    String[] menuItems = getResources().getStringArray(R.array.menu); 
    String menuItemName = menuItems[menuItemIndex]; 
    String listItemName = Customers[info.position]; 

    if (item.getTitle().toString().equalsIgnoreCase("Delete")) { 
     Toast.makeText(
       context, 
       "Selected List item is: " + listItemName + "MenuItem is: " 
         + menuItemName, Toast.LENGTH_LONG).show(); 

     DB = context.openOrCreateDatabase("CustomerDetails.db", 
       MODE_PRIVATE, null); 
     try { 
      int pos = info.position; 
      pos = pos + 1; 
      Log.d("", "customers[pos]: " + Customers[info.position]); 
      Cursor c = DB 
        .rawQuery(
          "Select customer_id,first_name,last_name from CustomerInfo", 
          null); 
      int rowCount = c.getCount(); 

      DB.delete(Table_name, 
        "customer_id" + "=" + String.valueOf(pos), null); 

      DB.close(); 
      Log.d("", "" + String.valueOf(pos)); 
      Toast.makeText(context, "Deleted Customer", Toast.LENGTH_LONG) 
        .show(); 
      // Customers[info.position]=null; 
      getCustomers(); 
     } catch (Exception e) { 
      Toast.makeText(context, "Delete unsuccessfull", 
        Toast.LENGTH_LONG).show(); 
     } 

    } 

這是我的logcat,

07-02 10:12:42.976: D/Cursor(1560): Database path: CustomerDetails.db 
07-02 10:12:42.976: D/Cursor(1560): Table name : null 
07-02 10:12:42.984: D/Cursor(1560): Database path: CustomerDetails.db 
07-02 10:12:42.984: D/Cursor(1560): Table name : null 

不知道爲什麼數據沒有被刪除的原因。數據存在於表中。

請更正我的代碼。 任何幫助表示讚賞!

+0

是什麼''TABLE_NAME的值,你不需要指定數據庫名稱.db的推廣。 – Lucifer

+0

@Lucifier,我這樣指定Table_name的值:String Table_name =「CustomerInfo」;它是一個全球性聲明。 – Mahe

+0

雅,我認爲是這樣,你的customer_id字段的數據類型是什麼? – Lucifer

回答

1

你CUSTOMER_ID爲整數類型,而在你把它當作字符串類型在下面一行,

DB.delete(Table_name,"customer_id" + "=" + String.valueOf(pos), null); 

,這就是爲什麼它返回false,自己嘗試使用它作爲整數如下,

DB.delete(Table_name,"customer_id" + "=" + pos, null); 
+0

謝謝。很好的幫助。我沒有觀察到這一點。如果拋出至少一個異常,我會糾正自己。 – Mahe

+0

但它是如何與模擬器一起工作的? – Mahe

+0

仿真器和真實設備都是不同的環境,仿真器從來沒有100%保證給真正的輸出 – Lucifer

0

如果可能,請嘗試此方法。

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 

public class RegistrationAdapter { 
    SQLiteDatabase database_ob; 
    RegistrationOpenHelper openHelper_ob; 
    Context context; 

    public RegistrationAdapter(Context c) { 
     context = c; 
    } 

    public RegistrationAdapter opnToRead() { 
     openHelper_ob = new RegistrationOpenHelper(context, 
       openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION); 
     database_ob = openHelper_ob.getReadableDatabase(); 
     return this; 

    } 

    public RegistrationAdapter opnToWrite() { 
     openHelper_ob = new RegistrationOpenHelper(context, 
       openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION); 
     database_ob = openHelper_ob.getWritableDatabase(); 
     return this; 

    } 

    public void Close() { 
     database_ob.close(); 
    } 

    public long insertDetails(String fname, String lname) { 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(openHelper_ob.FNAME, fname); 
     contentValues.put(openHelper_ob.LNAME, lname); 
     opnToWrite(); 
     long val = database_ob.insert(openHelper_ob.TABLE_NAME, null, 
       contentValues); 
     Close(); 
     return val; 

    } 

    public Cursor queryName() { 
     String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.FNAME, 
       openHelper_ob.LNAME }; 
     opnToWrite(); 
     Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols, null, 
       null, null, null, null); 

     return c; 

    } 

    public Cursor queryAll(int nameId) { 
     String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.FNAME, 
       openHelper_ob.LNAME }; 
     opnToWrite(); 
     Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols, 
       openHelper_ob.KEY_ID + "=" + nameId, null, null, null, null); 

     return c; 

    } 

    public long updateldetail(int rowId, String fname, String lname) { 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(openHelper_ob.FNAME, fname); 
     contentValues.put(openHelper_ob.LNAME, lname); 
     opnToWrite(); 
     long val = database_ob.update(openHelper_ob.TABLE_NAME, contentValues, 
       openHelper_ob.KEY_ID + "=" + rowId, null); 
     Close(); 
     return val; 
    } 

    public int deletOneRecord(int rowId) { 
     // TODO Auto-generated method stub 
     opnToWrite(); 
     int val = database_ob.delete(openHelper_ob.TABLE_NAME, 
       openHelper_ob.KEY_ID + "=" + rowId, null); 
     Close(); 
     return val; 
    } 

} 

openhelper.java

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 

public class RegistrationOpenHelper extends SQLiteOpenHelper { 
    public static final String DATABASE_NAME = "REGISTRATION_DB"; 
    public static final String TABLE_NAME = "REGISTRATION_TABLE"; 
    public static final String TABLE_NAME_ONE = "REGISTRATION_TABLE_ONE"; 
    public static final int VERSION = 1; 
    public static final String KEY_ID = "_id"; 
    public static final String FNAME = "F_NAME"; 
    public static final String PKEY_ID = "pid"; 
    public static final String PROFILE = "profile"; 
    public static final String LNAME = "L_NAME"; 
    public static final String SCRIPT = "create table " + TABLE_NAME + " (" 
      + KEY_ID + " integer primary key autoincrement, " + FNAME 
      + " text not null, " + LNAME + " text not null);"; 

    public static final String PROFILE_TABLE = "create table " + TABLE_NAME_ONE + " (" 
      + PKEY_ID + " integer primary key autoincrement, " + PROFILE 
      + " text not null,);"; 

    /* public static final String PROFILE_TABLE="create table profiletable(profileid integer primary key autoincrement,profilename text null);"; 
    public static final String VALUE_TABLE="create table valuetable(id integer primary key autoincrement,value text null,delay);"; 
    */ 


    public RegistrationOpenHelper(Context context, String name, 
      CursorFactory factory, int version) { 
     super(context, name, factory, version); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL(SCRIPT); 
     db.execSQL(PROFILE_TABLE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("drop table " + TABLE_NAME); 
     db.execSQL("drop table "+TABLE_NAME_ONE); 
     onCreate(db); 
    } 

}