-2

I have a database class in java where i'm trying to create a new table in sqlite. Tables get created when the app compiles but for some reason when i add a new line to add a new table it doesn't get created. My db class looks like this:

public static final String DATABASE_NAME = "something_db";

    private Lock writeLock;
    private boolean locked;

    private DataBase(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        writeLock = new ReentrantLock();
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        onCreateV1(db);
        onCreateV2(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        if(oldVersion == 1 && newVersion == 2)
        {
            onCreateV2(db);
        }
    }

    private void onCreateV1(SQLiteDatabase db)
    {
        db.execSQL("create table schedule (id integer, day integer, start text, end text, points double, unique(id) on conflict replace)");
        db.execSQL("create table zone(id integer, name text, imageUrl text, imageFileName text, points double, unique(id) on conflict replace)");
        db.execSQL("create table zoneLocation(zoneId integer, locationOrder integer, latitude double, longitude double, unique(zoneId, locationOrder) on conflict replace)");
        db.execSQL("create table userLocation(id integer primary key, timestamp integer, latitude double, longitude double, speed double)");
        db.execSQL("create table userPoint(id integer primary key, timestamp text, scheduleId integer, scheduleMultiplier double, speedRangeId integer, speedRangeMultiplier double, zoneId integer, zoneMultiplier double, latitude double, longitude double, total double, sent integer)");
        db.execSQL("create table userTransaction(userId integer, date text, points double, unique(userId, date) on conflict replace)");
        db.execSQL("create table speedRange(id integer, name text, lowerLimit double, points double, unique(id) on conflict replace)");

I tried adding a new line to create a new table but every time i compile the app it tells me

E/SQLiteLog: (1) no such table: UserTopChallengers

Shouldn't just be adding the line along the others table being created should be enough? Why is the table i added not being created with the others.

Luis
  • 305
  • 1
  • 14

1 Answers1

0

The onCreate method only runs once when the database is created. If you delete the App's data or uninstall the App then the database, being part of the App's data, will be deleted and the onCreate method will then run. Noting that deleting the App's data will lose any data that you may have.

Your onUpgrade method may or may not work (unable to say with the partial code supplied), probably the latter as you don't appear to be dropping the tables (e.g. DROP TABLE your_table_name;) so you will get errors due to the tables already existing - (CREATE TABLE IF NOT EXISTS ..... could circumvent this issue but even still any modifications e.g. new columns would not be added).

You will probably find it simpler to delete the App's data or uninstall the App and then rerun the App. Without the complete code a specific solution canot be given.

MikeT
  • 51,415
  • 16
  • 49
  • 68