Akash Akash - 3 months ago 7
Android Question

SQLiteOpenHelper not updating the DB even after DB version changed & uninstalled the Android App

I am stuck with my SQLiteOpenHelper class.

I created a DB and all my data from DB is on the app.

But later I added one column in one of the tables and now it says

no such column
in that table.

I have updated the DB version and also uninstalled the app and installed it again after updating the DB version, but it still shows the same issue:
no such column
exists.

If I comment Column:type, then it says
no such column named desc


I recreated the whole DB structure in a new file but still stuck in same issue.

Help me out, guys!

public static final String TABLE_RESTAURANTS = "restaurants";
public static final String COLUMN_LID = "id";
public static final String COLUMN_LNAME = "name";
public static final String COLUMN_LDESC = "desc";
public static final String COLUMN_LTYPE = "type";


In onCreate

String CREATE_TABLE_RESTAURANTS = "CREATE TABLE " + TABLE_RESTAURANTS + "("
+ COLUMN_LID + " INTEGER PRIMARY KEY," + COLUMN_LNAME + "TEXT,"
+ COLUMN_LDESC + "TEXT," + COLUMN_LTYPE + "TEXT" + ")";
db.execSQL(CREATE_TABLE_RESTAURANTS);


In onUpgrade

db.execSQL("DROP TABLE IF EXISTS"+ TABLE_RESTAURANTS);
onCreate(db);


Insertion method

public void insertRestaurantItsms(AppetiserData cartData)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_LNAME, cartData.getName());
contentValues.put(COLUMN_LDESC, cartData.getDescription());
contentValues.put(COLUMN_LTYPE, cartData.getCategory());

// Inserting Row
db.insert(TABLE_RESTAURANTS, null, contentValues);

db.close(); // Closing database connection
}


Fetch method

public List<AppetiserData> getAllDatarl(){
List<AppetiserData> dataList = new ArrayList<>();

// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_RESTAURANTS;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
AppetiserData data = new AppetiserData();

data.setName(cursor.getString(1));
data.setDescription(cursor.getString(2));

// Adding data to list
dataList.add(data);
} while (cursor.moveToNext());
}

return dataList;
}

Answer

Off course you had missed the space and you have also missed to put the semi-colon at the end of the query. Below I have added those changes to your query and posted.

Create Table Query:

String CREATE_TABLE_RESTAURANTS = "CREATE TABLE IF NOT EXISTS " + TABLE_RESTAURANTS + " ( "
            + COLUMN_LID + " INTEGER PRIMARY KEY, " + COLUMN_LNAME + " TEXT, "
            + COLUMN_LDESC + " TEXT, " + COLUMN_LTYPE + " TEXT " + ");";

Drop Table:

db.execSQL("DROP TABLE IF EXISTS "+ TABLE_RESTAURANTS);

Hope this is helpful :)