【问题标题】:Android Studio SQLITE Database is read only if I add drop database function in sqlhelper class如果我在 sqlhelper 类中添加删除数据库函数,则 Android Studio SQLITE 数据库是只读的
【发布时间】:2022-12-18 14:25:59
【问题描述】:

在我的 dbhelper 类中添加 drop 数据库函数给我数据库只读错误,我可能会尝试删除所需的表,但我想知道我在这里做错了什么,我尝试删除 drop 函数及其之后的工作

这是错误消息: 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);
}

}

【问题讨论】:

    标签: java android android-sqlite sql-insert sqliteopenhelper


    【解决方案1】:

    当助手打开数据库(SQLiteDatabase db=this.getWritableDatabase(); 强制打开文件)时,您正在从助手中删除数据库(文件)。

    因此,它打开的文件不再存在。您不太可能需要删除数据库,而是删除 (DROP) 表,然后调用 onCreate 方法。

    但是,管理模式更改的典型方法是通过 onUpdate 方法(至少对于分布式/发布应用程序)。当版本号(Super 调用的第 4 个参数)增加时调用此方法。

    如果开发应用程序,那么您也可以直接卸载应用程序(删除数据库文件),然后重新运行。

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多