简体   繁体   English

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

[英]Deleting or updating an entry in an SQLite database in Android

I have been working on this app for most of the day and created an SQLite database that store customer information.我一天中的大部分时间都在开发这个应用程序,并创建了一个存储客户信息的 SQLite 数据库。 I have options to update and delete items from the SQLite database and am not able to figure out why it is not working (or how it is supposed to work).我可以选择更新和删除 SQLite 数据库中的项目,但无法弄清楚它为什么不工作(或它应该如何工作)。 I can add to the database, but am not capable of editing or deleting information.我可以添加到数据库,但不能编辑或删除信息。 I also run into the issue when displaying specific information when I click on a listview item.当我单击列表视图项时显示特定信息时,我也遇到了这个问题。 The app crashses when I attempt to delete :当我尝试删除时应用程序崩溃:

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

this is the helper :这是帮手:

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

} }

I have been at this for way too long and may be easily overlooking something but I am currently at a breaking point.. I am assuming once I figure out how to delete or update my current SQLite database things will get easier from there but I have tried everything, at least is seems I have.我已经在这里待了太久了,可能很容易忽略一些东西,但我目前正处于一个突破点..我假设一旦我弄清楚如何删除或更新我当前的 SQLite 数据库,事情就会变得更容易,但我有尝试了一切,至少似乎我有。 I am guessing that part of the issue is what I display when I click on a list view item since it always displays the first item of my SQLite database.我猜测问题的一部分是我单击列表视图项时显示的内容,因为它始终显示我的 SQLite 数据库的第一项。 This code is :这段代码是:

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

and the Logcat which shows that it crashes right at 73 which is when I try to delete an item.和 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

For Update use this method to pass customerId of which Name you want to update对于更新,使用此方法传递要更新的 Name 的 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);
    }

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

This is what I used to complete a similar project这是我用来完成类似项目的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM