Only add column if it doesn't exist yet
Column can exist if there down- and upgrades
This commit is contained in:
parent
3b3458e196
commit
dc162a9ac8
|
@ -1,6 +1,7 @@
|
|||
package nodomain.freeyourgadget.gadgetbridge.database;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
|
@ -70,6 +71,13 @@ public class DBHelper {
|
|||
db.execSQL(statement);
|
||||
}
|
||||
|
||||
public static boolean existsColumn(String tableName, String columnName, SQLiteDatabase db) {
|
||||
try (Cursor res = db.rawQuery("PRAGMA table_info('" + tableName + "')", null)) {
|
||||
int result = res.getColumnIndex(columnName);
|
||||
return result != -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* WITHOUT ROWID is only available with sqlite 3.8.2, which is available
|
||||
* with Lollipop and later.
|
||||
|
|
|
@ -2,6 +2,7 @@ package nodomain.freeyourgadget.gadgetbridge.database.schema;
|
|||
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBUpdateScript;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_CUSTOM_SHORT;
|
||||
|
@ -13,9 +14,11 @@ import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.TABLE_GB
|
|||
public class ActivityDBUpdate_6 implements DBUpdateScript {
|
||||
@Override
|
||||
public void upgradeSchema(SQLiteDatabase db) {
|
||||
String ADD_COLUMN_CUSTOM_SHORT = "ALTER TABLE " + TABLE_GBACTIVITYSAMPLES + " ADD COLUMN "
|
||||
+ KEY_CUSTOM_SHORT + " INT;";
|
||||
db.execSQL(ADD_COLUMN_CUSTOM_SHORT);
|
||||
if (!DBHelper.existsColumn(TABLE_GBACTIVITYSAMPLES, KEY_CUSTOM_SHORT, db)) {
|
||||
String ADD_COLUMN_CUSTOM_SHORT = "ALTER TABLE " + TABLE_GBACTIVITYSAMPLES + " ADD COLUMN "
|
||||
+ KEY_CUSTOM_SHORT + " INT;";
|
||||
db.execSQL(ADD_COLUMN_CUSTOM_SHORT);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue