更新任意数量的列

Update any number of column

这是我的更新功能。它需要更新的所有参数。但是我希望更新功能能够根据用户传递的参数数量进行更新。

数据库中的函数声明

public boolean updateFeeder(int feederNo, int conductorCapacity, int total_load, int no_of_connection, 
int outgoing_line){   

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put("feederNo", feederNo);
        contentValues.put("conductorCapacity", conductorCapacity);
        contentValues.put("totalLoad", total_load);
        contentValues.put("totalNoOfConnection",no_of_connection);
        contentValues.put("outgoingLine", outgoing_line);


        db.update(TABLE_ADD_FEEDER, contentValues, "feederNo = ?", new String[]{Integer.toString(feederNo)});
        Cursor cursor = db.rawQuery("Select feederNo from ADD_FEEDER where feederNo = ?", new String[]{String.valueOf(feederNo)});
        if(cursor.getCount()>0)
            return true;
        else
            return false;
    }

函数调用

boolean isInserted = myDB.updateFeeder(Integer.parseInt(getFeederNo), Integer.parseInt(getConductorCapacity), Integer.parseInt(getTotalLoad), Integer.parseInt(getTotalNoOfConnection), Integer.parseInt(getOutgoingLine));

参数可以是字符串数组,可以在某些位置传null,在updateFeeder()位置不为null时设置contentValues,然后设置值。

您需要对每个位置执行此操作,因为您需要在 contentvalues.put

上指定列名称
 public boolean updateFeeder(String[] params) {
       .......
        ContentValues contentValues = new ContentValues();

        if (params[0] != null)
            contentValues.put("feederNo", Integer.parseInt(params[0]));

        if (params[1] != null)
            contentValues.put("conductorCapacity", Integer.parseInt(params[1]));
       ......
       // same per position
    }