英文:
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 < 2) { // or if (oldVersion == 1)
// write code here
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论