简体   繁体   English

Android Studio SQLITE 如果我在 sqlhelper class 中添加删除数据库 function,数据库是只读的

[英]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.

相关问题 Android Studio 无法读取我的 sqlite 数据库 - Android studio cannot read my sqlite database Android Studio Sqlite 数据库 - Android Studio Sqlite Database 仅当使用Sqlite数据库选中复选框时,如何才能将数据添加到另一个表中?-Android Studio - How To Add data to another table only if a checkbox is checked using Sqlite database?- android studio 如何将图像转换为字符串以将其读/写到 SQLite 数据库 - Android Studio (Java) - How do I convert an image to a String to read/write it to a SQLite Database - Android Studio (Java) Android Studio(SQLite 数据库),我要在表中添加一个 LineNo,我怎样才能让它自动递增 1? - Android Studio (SQLite Database), I am gonna to add a LineNo to a table and how can I make it will auto increment by 1? 在android studio中查看sqlite数据库 - view sqlite database in android studio 如何从android studio中的recycler视图将数据添加到SQLite数据库中 - How to add data into SQLite database from recycler view in android studio 我的 sqlite 数据库只插入一项 android studio (java) - My sqlite database is only inserting one item android studio (java) Android 工作室添加数据库 - Android Studio add a Database 如何将新列添加到Android SQLite数据库 - How can I add a new column to an Android SQLite Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM