自动升级数据库版本自动升级、版本、数据库

2023-09-07 03:53:28 作者:踩着棺材跳舞步

你好我创建存储用户在输入的密码的应用程序。每当他需要改变他的密码,然后将数据已被改变。我面临的问题是,当我调用DBHelper类(我创建做我​​所有的数据库工程)它不是删除表(如果可用)。

如果有人告诉我如何删除的条目表中同时调用DBHelper类这会更有帮助。

我意识到的OnCreate和OnUpgrade功能都没有,除了第一次执行时创建数据库。

也自动更改的数据库版本也有助于我想,但我不知道该怎么办?这里是从DBhelper类我的code

 公共无效的onCreate(SQLiteDatabase DB){        // TODO自动生成方法存根          db.execSQL(DROP TABLE IF EXISTS'+ DATABASE_TABLE +');          db.execSQL(CREATE TABLE+ DATABASE_TABLE +(+                KEY_ROWID +INTEGER PRIMARY KEY AUTOINCREMENT,+                KEY_ID +INTEGER,+                KEY_X +INTEGER,+                KEY_Y +INTEGER);                );    }    @覆盖    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){        // TODO自动生成方法存根        db.execSQL(DROP TABLE IF EXISTS'+ DATABASE_TABLE +');        的onCreate(DB);    }众长createEntry(INT名称,诠释SX,诠释SY){        // TODO自动生成方法存根        ContentValues​​ CV =新ContentValues​​();        cv.put(KEY_ID,名);        cv.put(KEY_X,SX);        cv.put(KEY_Y,SY);        返回ourDatabase.insert(DATABASE_TABLE,空,CV);    } 

解决方案 Bugzilla的安装和配置

在数据库中更改的行应该不会影响架构版本。这应该是正确的查询操作数据库的条目中执行。

您可能会发现使用这种方法:

 公共无效clearDatabase(上下文的背景下){   DatabaseHelper帮手=新DBHelper(背景);   SQLiteDatabase数据库= helper.getWritableDatabase();   database.delete(DATABASE_TABLE,NULL,NULL); //删除表中的所有内容。   database.close();} 

Hi i am creating an app which stores the password of the user entering. and whenever he needs to change his password then the data has to be changed. The problem i am facing is while i am calling the DBHelper class (i created to do all my database works) its not deleting the table if it is available.

It will be more helpful if someone tells me how to delete the entries in the table while calling the DBHelper class.

I realized that the OnCreate and OnUpgrade functions are not executing except the first time while creating the database.

also changing database version automatically also helps i guess but i don't know how to do?? here is my code from DBhelper class

public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
          db.execSQL("DROP TABLE IF EXISTS '" + DATABASE_TABLE+"'");
          db.execSQL("CREATE TABLE " + DATABASE_TABLE +" (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
                KEY_ID +" INTEGER, "+
                KEY_X +" INTEGER,  "+
                KEY_Y +" INTEGER);"
                );  

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS '" + DATABASE_TABLE+"'");
        onCreate(db);
    }

public long createEntry(int name, int sx, int sy) {
        // TODO Auto-generated method stub
        ContentValues cv=new ContentValues();
        cv.put(KEY_ID, name);
        cv.put(KEY_X, sx);
        cv.put(KEY_Y, sy);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);
    }

解决方案

Changing a row in the database should not affect the schema version. This should be executed as proper queries to manipulate the database entries.

You might find use of this method:

public void clearDatabase(Context context) {
   DatabaseHelper helper = new DBHelper(context);
   SQLiteDatabase database = helper.getWritableDatabase();
   database.delete(DATABASE_TABLE, null, null); //erases everything in the table.
   database.close();
}

 
精彩推荐
图片推荐