[英]Android Studio SQLITE Database is read only if I add drop database function in sqlhelper class
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在我的 dbhelper class 中添加 drop database class 给我错误的数据库只读,我可能会尝试删除所需的表,但我想知道我在这里做错了什么,我尝试删除 drop function 及其之后的工作
here is the error message: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032 SQLITE_READONLY_DBMOVED[1032])这是错误消息:android.database.sqlite.SQLiteReadOnlyDatabaseException:尝试写入只读数据库(代码 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).当帮助程序打开数据库(
SQLiteDatabase db=this.getWritableDatabase();
强制打开文件)时,您正在从帮助程序中删除数据库(文件)。
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 the onCreate
Method.您不太可能需要删除数据库,而是删除 (
DROP
) 表然后调用onCreate
方法。
However, the typical method of managing changes to the schema is via the onUpdate
method (at least for ditributed/publish Apps).但是,管理架构更改的典型方法是通过
onUpdate
方法(至少对于分布式/发布应用程序)。 This method is called when the version number (4th parameter of the Super call) is increased.当版本号(Super 调用的第 4 个参数)增加时调用此方法。
If developing the App, then you may as well just uninstall the App (which deletes the database file) and then rerun.如果开发应用程序,那么您也可以直接卸载应用程序(删除数据库文件),然后重新运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.