I'm writing an Android App in Java and I've changed my Database and now I want to migrate it to the new version but to do that I need to compute some of the old values and then insert those into one of the new tables into the db but when I call
database.execSQL()
with my variables and then access them it appears to not have set them as every value is 0.
Is it just not possible or did I simply miss something?
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("CREATE TABLE 'season' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
'spring' INTEGER DEFAULT 0, 'summer' INTEGER DEFAULT 0, 'autumn' INTEGER
DEFAULT 0, 'winter'
INTEGER DEFAULT 0, 'spook' INTEGER DEFAULT 0)");
database.execSQL("INSERT INTO 'Clothes' (type_of_clothing,clothing_name,description,seasonId,in_laundry,TypeList) " +
"SELECT type_of_clothing,clothing_name,description,type_of_clothing,in_laundry,TypeList " +
"FROM ClothesNew");
Cursor cursor = database.query("SELECT * FROM Clothes");
Cursor secondCursor = database.query("SELECT * FROM ClothesSeason");
ArrayList<Season> seasonsToBeAdded = new ArrayList<>();
while (cursor.moveToNext()) {
secondCursor.moveToNext();
int Sid = cursor.getInt(0);
String seasonString = secondCursor.getString(2);
Season season = new Season(seasonString);
int tempId = Sid;
boolean isInList = false;
for (Season tempSeason : seasonsToBeAdded) {
if (tempSeason.equals(season)) {
tempId = seasonsToBeAdded.indexOf(tempSeason);
isInList = true;
break;
}
}
if (!isInList) {
season.setId(seasonsToBeAdded.size());
tempId = season.getId();
seasonsToBeAdded.add(season);
}
if (seasonsToBeAdded.size() == 0) {seasonsToBeAdded.add(season);}
database.execSQL("UPDATE Clothes SET seasonId = :tempId WHERE uid = :Sid");
}
int id, spring, summer, autumn, winter, spook;
for (Season season : seasonsToBeAdded) {
id = season.getId();
spring = season.getSpring();
summer = season.getSummer();
autumn = season.getAutumn();
winter = season.getWinter();
spook = season.getSpook();
database.execSQL("INSERT INTO season " +
"VALUES(:id, :spring, :summer, :autumn, :winter, :spook) ");
}
Cursor otherCursor = database.query("SELECT * FROM season");
ArrayList<Integer> springValues= new ArrayList<>();
while (otherCursor.moveToNext()) {
springValues.add(otherCursor.getInt(1));
}
}
When I look at the Values stored in springValues they are all 0 even though I add the correct ones in this query
database.execSQL("INSERT INTO season " +
"VALUES(:id, :spring, :summer, :autumn, :winter, :spook) ");
Can I just not use the :var notation here?