This question already has an answer here:
I'm trying to update my database and I need to add a column to a table, TABLE_GASTO, the problem is that when I execute the app the column is not being add and I get the error
"java.lang.IllegalArgumentException: column 'MOEDA' does not exist"
when I executegasto.setMoedaMes(cursor.getString(cursor.getColumnIndexOrThrow(col_MOEDA)));
after a SELECT
.
DataBase:
private static final String UPD_GASTOS_EURO = "UPDATE " + TABLE_GASTOS + " (" + col_MOEDA + ")" + " VALUES('Euro');";
private static final String COUNT_GASTOS = "SELECT COUNT(*) FROM "+ TABLE_GASTOS + " WHERE " +col_MOEDA+" IS NULL;";
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CURRENCY);
db.execSQL("ALTER TABLE "+TABLE_GASTOS+" ADD COLUMN "+col_MOEDA+";");
db.execSQL("INSERT INTO " + TABLE_CURRENCY + " (" + col_MOEDA + ") VALUES (" + Euro + ");");
Cursor cursor = db.rawQuery(COUNT_GASTOS, null);
if(cursor != null){
cursor.moveToFirst();
if(cursor.getCount()>0)
db.execSQL(UPD_GASTOS_EURO);
}
}
Chamada :
public List<Gasto> getAllGastos(){
List<Gasto> gastos = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_GASTOS;
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor !=null) {
while (cursor.moveToNext()) {
Gasto gasto = new Gasto();
gasto.setMes(cursor.getString(cursor.getColumnIndex(col_MES)));
gasto.setAno(cursor.getInt(cursor.getColumnIndex(col_ANO)));
gasto.setMeta(cursor.getInt(cursor.getColumnIndex(col_META)));
gasto.setDespesa_final(cursor.getDouble(cursor.getColumnIndex(col_DESPESA_FINAL)));
gasto.setDespesa(cursor.getDouble(cursor.getColumnIndex(col_DESPESA)));
gasto.setMoedaMes(cursor.getString(cursor.getColumnIndexOrThrow(col_MOEDA)));
gasto.setData_cria(new Date(cursor.getLong(4)));
gasto.setData_modif(new Date(cursor.getLong(4)));
gastos.add(gasto);
}
}
db.close();
return gastos;
}
Can you help me?
Aucun commentaire:
Enregistrer un commentaire