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");
}
}
現有數據庫的表但當我嘗試添加使用addcolumn()
方法我的應用程序崩潰列。 如果我的代碼有問題,請幫我修改它。在我的Android應用我想將列添加到使用下面的方法
「我的應用程序崩潰」 - 請發佈您的LogCat輸出 – Droidman
LogCat輸出? –
如果我在評論後運行代碼: –