[英]Android adding new field to all records in an sqlite database
我創建了一個使用sqlite數據庫保存用戶條目(名稱和電話號碼)的應用。 發布該應用程序的第一個版本后,我決定發布版本2,並將另一個字段添加到數據庫(Age)。 不幸的是,人們已經安裝了該應用程序,並且每個人都有自己的條目(據我所知,可能是數百個條目)。 現在,當我運行較新的版本時,它崩潰了,因為較舊的條目沒有“ Age”字段,並且無法加載記錄。 我基本上想要的是在用戶更新時運行一段代碼,這將為用戶已經創建的所有記錄在“年齡”字段下添加值0。
我使用了此鏈接 , 該鏈接表示要使用此代碼ALTER TABLE mytable ADD COLUMN mycolumn TEXT
向數據庫添加新列。
而且我使用了此鏈接 ,它說我必須從database.update("contacts", addContactsAge, "_id=" + id, null);
刪除where
子句database.update("contacts", addContactsAge, "_id=" + id, null);
以便更新所有記錄。
我已經應用了所有這些功能,但是當我啟動新版本時,它立即崩潰了。
這是我完整的更新代碼:
public class DatabaseConnector {
private static final String DATABASE_NAME = "UserContacts";
private SQLiteDatabase database;
private DatabaseOpenHelper databaseOpenHelper;
public DatabaseConnector(Context context) {
databaseOpenHelper = new DatabaseOpenHelper(context, DATABASE_NAME,
null, 2);
}
public void open() throws SQLException {
database = databaseOpenHelper.getWritableDatabase();
}
public void close() {
if (database != null)
database.close();
}
public void insertContact(String name, String phone, String age) {
ContentValues newContact = new ContentValues();
newContact.put("name", name);
newContact.put("phone", phone);
newContact.put("age", age);
open();
database.insert("contacts", null, newContact);
close();
}
public void updateContact(long id, String name, String phone, String age) {
ContentValues editContact = new ContentValues();
editContact.put("name", name);
editContact.put("phone", phone);
editContact.put("age", age);
open();
database.update("contacts", editContact, "_id=" + id, null);
close();
}
public void setContactsAge() {
ContentValues addContactsAge = new ContentValues();
addContactsAge.put("age", "0");
open();
database.update("contacts", addContactsAge, null, null);
close();
}
public Cursor getAllContacts() {
return database.query("contacts",
new String[] { "_id", "name" }, null, null, null, null,
"name");
}
public Cursor getOneContact(long id) {
return database.query("contacts", null, "_id=" + id, null, null, null,
null);
}
public void deleteContact(long id) {
open();
database.delete("contacts", "_id=" + id, null);
close();
}
private class DatabaseOpenHelper extends SQLiteOpenHelper {
public DatabaseOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createQuery = "CREATE TABLE contacts"
+ "(_id integer primary key autoincrement,"
+ "name TEXT, phone TEXT, age TEXT);";
db.execSQL(createQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 1 && newVersion == 2)
database.execSQL("ALTER TABLE contacts ADD COLUMN age TEXT");
}
}
}
MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor editor = preferences.edit();
int i = preferences.getInt("numberoflaunches", 1);
if (i < 2) {
DatabaseConnector databaseConnector = new DatabaseConnector(MainActivity.this);
databaseConnector.setContactsAge();
i++;
editor.putInt("numberoflaunches", i);
editor.commit();
}
}
}
就像我說的那樣,這段代碼對我不起作用,我也不知道問題出在哪里。 任何幫助將不勝感激。
我已將數據庫版本更改為2,但是沒有任何運氣。 我還嘗試刪除了if (oldVersion == 1 && newVersion == 2)
。
刪除線
if (oldVersion == 1 && newVersion == 2)'
在onUpgrade
方法中,android自動檢測到一個新版本,您要做的就是每當對db進行更改時,在DatabaseOpenHelper
調用中增加該版本,完成后將舊表刪除並創建一個新表。
在DatabaseConnector類中更改數據庫版本
public DatabaseConnector(Context context) {
databaseOpenHelper = new DatabaseOpenHelper(context, DATABASE_NAME,
null, 2);
}
每當您需要在android應用中更新數據庫時,都需要增加數據庫版本,它將調用onUpgrade方法,您可以在其中更新數據庫。
您也可以嘗試此操作,通過預裝的db推送應用程序。 使用數據庫版本創建數據庫,並將其保存在資產文件夾中。 每當應用程序運行時,比較資產數據庫和應用程序數據庫的版本。 如果資產的數據庫版本高於應用程序的數據庫,則意味着您需要更新數據庫。 直接將資產db復制到文件夾構造函數中,並發送增量調用以獲取數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.