public void addcolumn(String year,String period){
//Create column name here
Calendar now = Calendar.getInstance();
int yeara = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
//select period according to current time
String colnew = ""+yeara+"_"+month+"_"+day+"_"+period;
SQLiteDatabase db = this.getWritableDatabase();
String exec;
//Add column in table according to given year
//
if (year.equals("First Year")) {
exec="ALTER TABLE "+ table_name1+" ADD COLUMN "+colnew+" INTEGER NOT NULL DEFAULT 0";
db.execSQL(exec);
}
else if (year.equals("Second Year")) {
db.execSQL("ALTER TABLE "+ table_name2+" ADD COLUMN "+colnew+" INTEGER NOT NULL DEFAULT 0");
}
else if (year.equals("Third Year")) {
db.execSQL("ALTER TABLE "+ table_name3+" ADD COLUMN "+colnew+" INTEGER NOT NULL DEFAULT 0");
}
else if (year.equals("Fourth Year")) {
db.execSQL("ALTER TABLE "+ table_name4+" ADD COLUMN "+colnew+" INTEGER NOT NULL DEFAULT 0");
}
}
But when I try to add columns using the addcolumn()
method my app crashes.
If there is something wrong with my code, please help me to correct it.