删除或更新Android中SQLite数据库中的条目

时间:2016-09-11 03:46:33

标签: android sqlite

我一直在大部分时间都在使用这个应用程序,并创建了一个存储客户信息的SQLite数据库。我有选项来更新和删除SQLite数据库中的项目,我无法弄清楚它为什么不工作(或它应该如何工作)。我可以添加到数据库,但无法编辑或删除信息。当我点击listview项目时,我也会在显示特定信息时遇到问题。当我尝试删除时,应用程序崩溃:

        Button addBtn1 = (Button)findViewById(R.id.deleteCust);
    addBtn1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){

            Cursor cursor = datasource.displayCurrentCustomer();
            if( cursor != null) {
                String where = CustDBOpenHelper.CustID + "=" + dbOpenHelper.CustID;
                database.delete(CustDBOpenHelper.TABLE_NAME, where, null);

                startActivity(new Intent(EditCustomers.this, CustomerView.class));
            }
        }
    });

这是帮手:

public class CustDBOpenHelper extends SQLiteOpenHelper {

private static final String LOGTAG = "test";
private static final String DATABASE_NAME = "CARDEALER.db";
private static final int DATABASE_VERSION = 1;

public static final String TABLE_NAME = "CUSTOMER_TABLE";
public static final String CustID = "cust_id";
public static final String FirstName = "first_name";
public static final String LastName = "last_name";
public static final String CarMake = "car_make";
public static final String CarModel = "car_model";
public static final String CarCost = "car_cost";

private static final String TABLE_CREATE =
        " CREATE TABLE " + TABLE_NAME + " (" +
        CustID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        FirstName + " TEXT, "+
        LastName + " TEXT, " +
        CarMake + " TEXT, " +
        CarModel + " TEXT, " +
        CarCost + " NUMERIC " + ")";

public CustDBOpenHelper(Context context ) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db){
    db.execSQL(TABLE_CREATE);
    Log.i(LOGTAG, "Table created");
    }
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int NewVersion){
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

}

我已经在这方面花了太长时间,可能很容易忽略一些东西,但我目前处于一个突破点。我假设一旦我弄清楚如何删除或更新我当前的SQLite数据库的东西将变得更容易从那里但我已经尝试了一切,至少看起来我有。我猜这部分问题是我点击列表视图项时显示的内容,因为它始终显示我的SQLite数据库的第一项。这段代码是:

        List<Custs> custs = datasource.findAll();
    if (custs.size() == 0) {
        Toast.makeText(this,"No customer information to show", Toast.LENGTH_SHORT).show();
    }

    ArrayAdapter<Custs> adapter = new ArrayAdapter<Custs>(this, android.R.layout.simple_list_item_1 , custs);

    ListView lv = (ListView) findViewById(R.id.custListView);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


            startActivity(new Intent(CustomerView.this , EditCustomers.class));
        }
    });

和Logcat,它显示它在73处崩溃,这是我尝试删除项目时。

09-10 20:54:25.497 6153-6153/mbl402.phoenix.edu.week4appni4992 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: mbl402.phoenix.edu.week4appni4992, PID: 6153
                                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.delete(java.lang.String, java.lang.String, java.lang.String[])' on a null object reference
                                                                                 at mbl402.phoenix.edu.week4appni4992.EditCustomers$2.onClick(EditCustomers.java:73)
                                                                                 at android.view.View.performClick(View.java:5198)
                                                                                 at android.view.View$PerformClick.run(View.java:21147)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


                                                                             --------- beginning of system

2 个答案:

答案 0 :(得分:0)

更新 使用此方法传递要更新其名称的customerId

    public void updateFirstName(CustId custid, String firstname) {
       final SQLiteDatabase db = this.getWritableDatabase();
       final String[] args = {custid};
       ContentValues values = new ContentValues();
       values.put(CustDBOpenHelper.FirstName, firstname);
       db.update(CustDBOpenHelper.TABLE_NAME, values, CustDBOpenHelper.CustID + "=?", args);
    }

对于DELETE

   public boolean deleteCustomer(CustId custid) {
       final SQLiteDatabase db = this.getWritableDatabase();
       final String[] args = {custid};
       return db.delete(CustDBOpenHelper.TABLE_NAME, CustDBOpenHelper.CustID + "=?", args) > 0;
    }

答案 1 :(得分:0)

 public boolean UpdateData(String ID, String First, String Last, String  MAKE, String MODEL, String Cost) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();

    cv.put(CUSTOMER_FIRST_NAME, First);
    cv.put(CUSTOMER_LAST_NAME, Last);
    cv.put(CAR_MAKE, MAKE);
    cv.put(CAR_MODEL, MODEL);
    cv.put(COST, Cost);
    db.update(Table_name, cv, "ID = ?", new String[]{ID});
    return true;
}

public Integer deleteData(String ID) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(Table_name, "ID =?", new String[]{ID});
}

这是我用来完成类似项目的原因