Search code examples
androidandroid-sqlite

During printing result that get during db.update() prints 0


During printing updated value it always returns 0 and database is not updated. I want to update this database. Is there any another process to update this database. Database is updated by using this method or not?

public void upgradealldata(String latco, String lngco,
    String locationco, String coll,String conv,String remark, 
    String del_activity,String service_issues,String comp_activity, 
    String image, String image1,String order_details,String 
    closing_stock,String checkout_time,String distance_travelled, 
    String payimg, String payrem)
    {
    SQLiteDatabase db = getWritableDatabase();
    try
     {
    ContentValues cvalues = new ContentValues();
    cvalues.put(LATITUDE_CHKOUT, latco);
    cvalues.put(LONGITUDE_CHKOUT, lngco);
    cvalues.put(LOCATION_CHKOUT, locationco);
    cvalues.put(COLLECTION, coll);
    cvalues.put(CONVEYANCE, conv);
    cvalues.put(REMARK, remark);
    cvalues.put(SERVICE_ISSUES, service_issues);
    cvalues.put(IMAGE, image);
    cvalues.put(IMAGE1, image1);
    cvalues.put(ORDER_DETAILS, order_details);
    cvalues.put(CLOSING_STOCK, closing_stock);
    cvalues.put(CHECKOUT_TIME, checkout_time);
    cvalues.put(DISTANCE_TRAVELLED, distance_travelled);
    cvalues.put(DEL_ACTIVITY, del_activity);
    cvalues.put(COMP_ACTIVITY, comp_activity);
    cvalues.put(CHO_PYMENTIMAGE, payimg);
    cvalues.put(CHO_PAYMENTREMARK, payrem);

    int result = db.update(TABLE_ALLSENDDETAILS,cvalues,
    LATITUDE_CHKOUT+" =? and "+LONGITUDE_CHKOUT+" =? and
    "+LOCATION_CHKOUT+" =? and "+COLLECTION+" =? and
    "+CONVEYANCE+"=? and "+REMARK+" =? and "+SERVICE_ISSUES+" =?
    and "+IMAGE+" =? and "+IMAGE1+" =? and "+ORDER_DETAILS+" =?
    and "+CLOSING_STOCK+" =? and "+CHECKOUT_TIME+" =? and
    "+DISTANCE_TRAVELLED+" =? and "+DEL_ACTIVITY+" =? and
    "+COMP_ACTIVITY+" =? and "+CHO_PYMENTIMAGE+" =? and 
    "+CHO_PAYMENTREMARK+" =?",new String[]{
    latco,lngco,locationco,coll,conv,remark,service_issues,
    image,image1,order_details,closing_stock,checkout_time,
    distance_travelled,del_activity, comp_activity,payimg,payrem
}

);
    System.out.println("UpdateValue====>"+String.valueOf(result));
    db.close();
    }
    catch (SQLiteException e){
     e.printStackTrace();
    }
    finally{
    db.close();
    }
    }

Solution

  • The values that you have passed to the method (i.e. the values that you want to update a row with) are the same values that you are using to identify the row to be updated.

    You would want something along the lines of :-

    public void upgradealldata(String latco, String lngco, String 
        locationco, String coll,String conv, String remark,                               
        String del_activity, String service_issues, String comp_activity,
        String image, String image1, String order_details, String 
        closing_stock,String checkout_time, String distance_travelled, 
        String payimg, String payrem,
    
    
        // ADDED parameters for the original values of the row
        String old_latco, String old_lngco, String 
        old_locationco, String old_coll,String old_conv, String old_remark,                               
        String old_del_activity, String old_service_issues, String old_comp_activity,
        String old_image, String old_image1, String old_order_details, String 
        old_closing_stock,String old_checkout_time, String old_distance_travelled, 
        String old_payimg, String old_payrem)
    {      
        SQLiteDatabase db = getWritableDatabase();
    
        try
        {
           ContentValues cvalues = new ContentValues();
           cvalues.put(LATITUDE_CHKOUT, latco);
           cvalues.put(LONGITUDE_CHKOUT, lngco);
           cvalues.put(LOCATION_CHKOUT, locationco);
           cvalues.put(COLLECTION, coll);
           cvalues.put(CONVEYANCE, conv);
           cvalues.put(REMARK, remark);
           cvalues.put(SERVICE_ISSUES, service_issues);
           cvalues.put(IMAGE, image);
           cvalues.put(IMAGE1, image1);
           cvalues.put(ORDER_DETAILS, order_details);
           cvalues.put(CLOSING_STOCK, closing_stock);
           cvalues.put(CHECKOUT_TIME, checkout_time);
           cvalues.put(DISTANCE_TRAVELLED, distance_travelled);
           cvalues.put(DEL_ACTIVITY, del_activity);
           cvalues.put(COMP_ACTIVITY, comp_activity);
           cvalues.put(CHO_PYMENTIMAGE, payimg);
           cvalues.put(CHO_PAYMENTREMARK, payrem);
    
           int result = db.update(TABLE_ALLSENDDETAILS,cvalues, 
           LATITUDE_CHKOUT+" =? and "+LONGITUDE_CHKOUT+" =? and 
           "+LOCATION_CHKOUT+" =? and "+COLLECTION+" =? and "+CONVEYANCE+" 
           =? and "+REMARK+" =? and "+SERVICE_ISSUES+" =? and "+IMAGE+" =? 
           and "+IMAGE1+" =? and "+ORDER_DETAILS+" =? and 
           "+CLOSING_STOCK+" =? and "+CHECKOUT_TIME+" =? and 
           "+DISTANCE_TRAVELLED+" =? and "+DEL_ACTIVITY+" =? and 
           "+COMP_ACTIVITY+" =? and "+CHO_PYMENTIMAGE+" =? and 
           "+CHO_PAYMENTREMARK+" =?",
    
            new String[{old_latco,old_lngco,old_locationco,old_coll,old_conv,old_remark,old_service_issues,old_image,old_image1,old_order_details,old_closing_stock,old_checkout_time,old_distance_travelled,old_del_activity,old_comp_activity,old_payimg,payrem});
            System.out.println("UpdateValue====>"+String.valueOf(result));
            db.close();
    
        } catch (SQLiteException e)
        {
           e.printStackTrace();
        } finally {
            db.close();
        }
    
    }
    
    • and then pass the values of the row to be updated as the parameters prefixed with old_.

    However, this is one of the reasons why an id column (that is a unique identifier, such as an alias of the rowid) is frequently used so you can identify a specific row just by a single value.

    Additional

    Re comment

    This table consists of 28 columns first of all during inserting operation first 5 columns values inserted and rest column insert blank data after that i want to update rest 23 column with data.How is it possible here?

    That confirms that the issue you are having is that the WHERE clause will not locate the row to be updated and hence why the update returns 0 (i.e. 0 rows have been updated).

    In short you must be able to identify the row that you want to update.

    Possible solution 1.

    Identify the row to update if you have a UNIQUE column or UNIQUE combined columns

    If any of the 5 columns are defined with UNIQUE then you could use that column in the WHERE clause e.g. (assuming for the example that lacto were defined as lacto TEXT UNIQUE) then you could use :-

    public void upgradealldata( string lacto, String lngco, .... the other columns..... Strain payrem,
    
        String old_lacto) {
    
        ..... code for setting the ContentValues as it is .....
    
        String whereclausae = LATITUDE_CHKOUT+" =?";
        String[] whereargs = new String[]{old_lacto};
        int result = db.update(TABLE_ALLSENDDETAILS,cvalues,whereclause,whereargs);
        System.out.println("UpdateValue====>"+String.valueOf(result));
        db.close();
    }
    

    If a number of the 5 columns combined are either defined as being UNIQUE or will definitely be UNIQUE as the combination, then you can pass the original (old_) values to the method and combine those columns for the WHERE clause and arguments.

    Possible Solution 2

    Identify the row to update via the rowid or an alias of the rowid

    Table must not have been defined as a WITHOUT ROWID table.

    If none of the columns or column combinations will definitely always be UNIQUE then you can use the row's rowid column (as long as you haven't the defined the table as being a without rowid table (i.e. the table is defined with the WITHOUT ROWID keywords)).

    If you have a column defined as the_column_name INTEGER PRIMARY KEY or the_column_name INTEGER PRIMARY KEY AUTOINCREMENT then the column will be an alias of the rowid column so the_column_name can be used.

    • *the_column_name is not the actual name it represents any valid column name. e.g. you might have id INTEGER PRIMARY KEY so id is the column name represented by the_column_name.

    So you in either case you have a column name that will identify 1 specific row.

    Getting the value of the rowid or alias from the inserted row to be updated

    You can get this value when you insert the row using the SQLiteDatabase insert method as it is the value returned from the insert e.g long id_of_the_insert_row = db.insert(..... or you can run the query SELECT last_insert_rowid(); e.g.

    log id_of_the_last_inserted_row = 0;
    Cursor csr = db.rawQuery("SELECT last_insert_rowid()",null);
    if (csr.moveToFirst()) {
        id_of_the_last_inserted_row = csr.getLong(0);
    } 
    

    The upgradealldata method could then be along the lines of :-

    public void upgradealldata( string lacto, String lngco, .... the other columns..... Strain payrem,
    
        long rowid_or_alias) {
    
        ..... code for setting the ContentValues as it is .....
    
        String whereclausae "rowid=?";
        String[] whereargs = new String[]{String.valueOf(rowid_or_alias)};
        int result = db.update(TABLE_ALLSENDDETAILS,cvalues,whereclause,whereargs);
        System.out.println("UpdateValue====>"+String.valueOf(result));
        db.close();
    }
    
    • The above would work wether or not you had an alias for the rowid column

    If you had an alias for the rowid column (let's say it was defined as a constant ID_COLUMN (e.g. you had public static final String ID_COLUMN = "id") then you could use :-

    public void upgradealldata( string lacto, String lngco, .... the other columns..... Strain payrem,
    
        long rowid_or_alias) {
    
        ..... code for setting the ContentValues as it is .....
    
        String whereclausae ID_COLUMN + "=?";
        String[] whereargs = new String[]{String.valueOf(rowid_or_alias)};
        int result = db.update(TABLE_ALLSENDDETAILS,cvalues,whereclause,whereargs);
        System.out.println("UpdateValue====>"+String.valueOf(result));
        db.close();
    }
    
    • Note ..... (a number of dots/full stops represents code as per the original code)
    • Additional Note The code above is in-principle code. It has not been tested and may contain errors.