Updating SQLite Database and prevent to reset all database

huangapple go评论76阅读模式
英文:

Updating SQLite Database and prevent to reset all database

问题

I've got an SQLite database. I would like to update this database to version 2.0. But the main key of my app is that some parts of data from database version 1.0 are modified by users and I don't want the situation that version 2.0 erase this data. What I want is to add some rows to the database and prevent resetting all databases. How can I do it?

public static final String DBNAME = "Animals.db";

@SuppressLint("SdCardPath")
public static final String DBLOCATION = "/data/data/com.erk.animals/databases/";
private Context mContext;
private SQLiteDatabase mDatabase;

public DatabaseHelper(Context context) {
    super(context, DBNAME, null, 1);
    this.mContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    // Implementation here if needed.
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Implementation here if needed.
}

public void openDatabase() {
    String dbPath = mContext.getDatabasePath(DBNAME).getPath();
    if (mDatabase != null && mDatabase.isOpen()) {
        return;
    }
    mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}

public void closeDatabase() {
    if (mDatabase != null) {
        mDatabase.close();
    }
}

(Note: This is the code you provided in your message.)

英文:

I've got an SQLite database. I would like to update this database to version 2.0. But the main key of my app is that some parts of data from database version 1.0 are modified by users and I don't want the situation that version 2.0 erase this data. What I want is to add some rows to database and prevent reset all databases. How can I do it?

public static final String DBNAME = "Animals.db";

@SuppressLint("SdCardPath")

public static final String DBLOCATION = "/data/data/com.erk.animals/databases/";
private Context mContext;
private SQLiteDatabase mDatabase;





public DatabaseHelper(Context context) {

    super(context, DBNAME, null, 1);
    this.mContext = context;
}


@Override
public void onCreate(SQLiteDatabase db) {


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


public void openDatabase() {

    String dbPath = mContext.getDatabasePath(DBNAME).getPath();
    if (mDatabase != null && mDatabase.isOpen()) {
        return;
    }
    mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}

public void closeDatabase() {

    if (mDatabase != null) {
        mDatabase.close();
    }

}

答案1

得分: 0

请将构造函数中的版本更改为2

super(context, DBNAME, null, 2);

尽管使用静态变量作为版本号是一个好的实践。

然后,您必须使用onUpgrade() 方法,并在其中编写代码以插入新的行:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 2) {
        // 在这里编写代码
    }
}
英文:

First change the version inside the constructor to 2:

super(context, DBNAME, null, 2);

Although it's a good practice to use a static variable for the version.<br/>

Then you must use onUpgrade() and write code inside it that inserts the new rows:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion &lt; 2) {  // or if (oldVersion == 1)
        // write code here
    }
}

huangapple
  • 本文由 发表于 2020年8月2日 20:12:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63215835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定