Reputation: 1978
I have changed some field of table Meeting and minutes in the database and changed Database version from 1 to 3. when i run the app on emulator i get error that the table still exists. How do i drop the table?
log cat:
05-31 15:30:32.338: E/AndroidRuntime(20347): android.database.sqlite.SQLiteException: table Meeting already exists: , while compiling: create table Meeting (_id integer primary key autoincrement, meet text not null, venue text not null, agenda text not null, time text not null, date text not null, phoneNo text not null, email text not null);
05-31 15:30:32.338: E/AndroidRuntime(20347): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
database code:
public class DBUserAdapter
{
public static final String KEY_ROWID = "_id";
private static final String TAG = "DBAdapter";
public static final String KEY_ROWID2 = "_id1";
private static final String DATABASE_NAME = "usersdb";
private static final String DATABASE_TABLE = "Meeting";
private static final String DATABASE_TABLE1 = "minutes";
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_CREATE =
"create table Meeting (_id integer primary key autoincrement, "
+ "meet text not null, "
+ "venue text not null, "
+ "agenda text not null, "
+ "time text not null, "
+ "date text not null, "
+ "phoneNo text not null, "
+ "email text not null);";
private static final String MINUTES_TABLE_CREATE =
"create table minutes (_id1 integer primary key autoincrement, "
+ "meet text not null, "
+ "minutes text not null,";
private SQLiteDatabase db;
public DBUserAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
db.execSQL(MINUTES_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS DATABASE_TABLE ");
db.execSQL("DROP TABLE IF EXISTS DATABASE_TABLE1 ");
onCreate(db);
}
}
Im confused how does the onupgrade() works ? what should i do? to drop the table?
Upvotes: 0
Views: 103
Reputation: 2541
Change this
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
ditto for other table. You want the value of DATABASE_TABLE not the string "DATABASE_TABLE"
Upvotes: 1