adding drop database function in my dbhelper class gives me error of database read only , I might try deleting the required tables but I would like to know what I am doing wrong here, I tried removing the drop function and its working after that
here is the error message : android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032 SQLITE_READONLY_DBMOVED[1032])
public class logdb extends SQLiteOpenHelper{
Context c;
SQLiteDatabase db=this.getWritableDatabase();
public logdb(@Nullable Context context)
{
super(context, "login.db", null, 1);
this.c = context;
try
{
String st = "create table if not exists user(email text,password text,username text)";
db.execSQL(st);
}
catch(Exception e){
}
}
@Override
public void onCreate(SQLiteDatabase db) {}
public void createuser()
{
try
{
String st = "create table if not exists user(email text,password text,username text)";
db.execSQL(st);
}
catch(Exception e)
{
}
}
public String drop(){
try
{
c.deleteDatabase("login.db");
//adding this line is turning the database into read only
}
catch(Exception e )
{
}
return " No here error ";
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {}
public void oninsert(ContentValues cv)
{
try
{
db.execSQL("insert into user values('"+cv.get("email")+"','"+cv.get("password")+"','"+cv.get("username")+"')");
}
catch(Exception e)
{
}
}
public String getusername(){
Cursor c = db.rawQuery("select * from user",null);
if(c.getCount()!=0)
{
c.moveToNext();
return c.getString(2);
}
return c.getString(2);
}
}
You are deleting the database (the file) from within the helper, when it has been opened by the helper (
SQLiteDatabase db=this.getWritableDatabase();force an open of the file).Thus the file that it opened no longer exists. It is very unlikely that you need to delete the database, rather delete (
DROP) the tables and then call theonCreateMethod.However, the typical method of managing changes to the schema is via the
onUpdatemethod (at least for ditributed/publish Apps). This method is called when the version number (4th parameter of the Super call) is increased.If developing the App, then you may as well just uninstall the App (which deletes the database file) and then rerun.