0

我正在构建一个简单的应用程序,它存储一些联系人并在 android 手机设备中检索联系人。

我已经创建了自己的数据库和表,并将值插入到手机中的表中。

我的手机没有root。所以我无法访问这些文件,但我看到值存储在表中。并在模拟器上进行了测试。到这里为止还好。

通过从表中获取数据来显示列表中的所有联系人。这也很好。

但问题是当我试图删除记录时,它在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();
        }

    }

这是我的日志猫,

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

不知道数据没有被删除的原因。数据存在于表中。

请更正我的代码。任何帮助表示赞赏!

4

2 回答 2

1

您的 customer_id 是整数类型,而您在下一行将其作为字符串类型传递,

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

这就是它返回的原因false,尝试将其用作整数本身,如下所示,

DB.delete(Table_name,"customer_id" + "=" + pos, null);
于 2013-07-02T05:03:14.857 回答
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);
    }

}
于 2013-09-12T12:10:06.270 回答