0

I need to update a row of my database, but I get an error with SQLiteDatabase update method not resolved. I think this is because of the custom class I had to create:

public class DBAccess {

public static String LOG_TAG = "DBAccess";

private SQLiteDatabase database;
private DBHelper dbHelper;


public DBAccess(Context context) {
    dbHelper = new DBHelper(context);
}

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}

public Integer saveVehicle (Vehicle v){
    Integer id = null;
    if(this.database.isOpen() && !this.database.isReadOnly()){
        String queryInsert = DBHelper.QueryAccessoAlDato.INSERT_VEHICLE;

        try{


            this.database.execSQL(queryInsert, new Object[]{v.getManufacturer(), v.getModel(), v.getPlate(),
                    v.getKmAmount(), v.getPrezzoGiorno(), v.getPrezzoSettimana(), v.getPrezzoMese(), v.getFuel(),
                    v.getGruppoMacchina()});
        }catch (SQLException e){
            Log.e(LOG_TAG, "Si รจ verificato un errore in inserimento " + e.getMessage());
            e.printStackTrace();
            return null;
        }

Then this is the OnClick method of the main activity:

@Override
public void onClick(View v) {

    Log.d(LOG_TAG, "Marca: "+this.etManufacturer.getText());
    Log.d(LOG_TAG, "Modello: "+this.etModel.getText());
    Log.d(LOG_TAG, "Targa: "+this.etPlate.getText());
    Log.d(LOG_TAG, "Kilometraggio: "+this.etKmAmount.getText());
    Log.d(LOG_TAG, "PrezzoGiorno: "+this.etPrezzoGiorno.getText());
    Log.d(LOG_TAG, "PrezzoSettimana: "+this.etPrezzoSettimana.getText());
    Log.d(LOG_TAG, "PrezzoMese: "+this.etPrezzoMese.getText());
    Log.d(LOG_TAG, "Fuel: "+this.etFuel.getText());
    Log.d(LOG_TAG, "Gruppo Macchina: "+this.etGruppoMacchina.getText());




    if(!(this.etManufacturer.getText().length() == 0)  &&
            !(this.etModel.getText().length() == 0) &&
            !(this.etPlate.getText().length() == 0) &&
            !(this.etKmAmount.getText().length() == 0) &&
            !(this.etPrezzoGiorno.getText().length() == 0) &&
            !(this.etPrezzoSettimana.getText().length() == 0) &&
            !(this.etPrezzoMese.getText().length() == 0) &&
            !(this.etFuel.getText().length() == 0) &&
            !(this.etGruppoMacchina.getText().length() == 0)
            ){

        Vehicle myVehicle = null;

        String manufacturer = this.etManufacturer.getText().toString();
        String model        = this.etModel.getText().toString();
        String plate        = this.etPlate.getText().toString();
        long KmAmount       = Long.parseLong(this.etKmAmount.getText().toString());
        int prezzoGiorno    = Integer.parseInt(etPrezzoGiorno.getText().toString());
        int prezzoSettimana = Integer.parseInt(etPrezzoSettimana.getText().toString());
        int prezzoMese      = Integer.parseInt(etPrezzoMese.getText().toString());
        String fuel         = this.etFuel.getText().toString();
        String gruppoMacchina = this.etGruppoMacchina.getText().toString();





        myVehicle = new Vehicle (manufacturer,  model,  plate, KmAmount, prezzoGiorno,
                prezzoSettimana, prezzoMese,fuel, gruppoMacchina);
        Log.d(LOG_TAG, "Hai aggiunto " + myVehicle + "con id" + vehicleID);




        DBAccess dba = new DBAccess(this.getApplicationContext()); 
        dba.open();
        Log.d(LOG_TAG, "Avvio lettura da db");

        if (this.getIntent().hasExtra(Const.ID_VEHICLE)) {

        //http://stackoverflow.com/questions/9798473/sqlite-in-android-how-to-update-a-specific-row
        ContentValues values= new ContentValues();
        values.put(DBHelper.VEHICLE_MANUFACTORER_COLUMN, manufacturer);
        values.put(DBHelper.VEHICLE_MODEL_COLUMN, model);
        //values.put(DBHelper.KEY_PEDLOCATION, ped_location);
        // values.put(DBHelper.KEY_PEDEMAIL, ped_emailid);
        // etc etc
        dba.update(DBHelper.TABLE_VEHICLE, values, DBHelper.VEHICLE_ID_COLUMN + "=" + vehicleID, null);
        finish();

        }else{

        dba.saveVehicle(myVehicle); 
        Log.d(LOG_TAG, "Ho salvato" + myVehicle);
        dba.close(); 


        finish();

So i get the error at line :

dba.update(DBHelper.TABLE_VEHICLE, values, DBHelper.VEHICLE_ID_COLUMN + "=" + vehicleID, null);

How can I resolve this? Thank you!

1 Answers1

0

First your DBAccess class should extend SQLiteOpenHelper class.

The Update method declaration is as below, the last parameter should be a String array,

public int update (String table, ContentValues values, String whereClause, String[] whereArgs)

Change your code to as shown below:

dba.update(DBHelper.TABLE_VEHICLE, values, DBHelper.VEHICLE_ID_COLUMN + "=?", new String[]{String.valueOf(vehicleID));
Psypher
  • 10,717
  • 12
  • 59
  • 83
  • I used your code, but the update word reamin stuck in red. I think it's not a problem concerning parameters. I think the problem is that update metthod cannot be found in DBAccess method โ€“ ciuschifranco Jul 09 '15 at 19:20