簡體   English   中英

更新SQLite數據庫Android

[英]Updating SQLite database android

好的,我創建了一個數據庫,我意識到每次更改數據庫中的某些內容時都必須先卸載然后重新安裝Application :(這非常令人沮喪...這是我數據庫的代碼,希望您能對我有所幫助!不知道我的代碼有什么問題:

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Cook_tab_snacks_data extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "Snacks";

public Cook_tab_snacks_data(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String sql = "CREATE TABLE IF NOT EXISTS snacks (" +
                    "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                    "name TEXT, " +
                    "disc TEXT, " +
                    "photo TEXT, " +
                    "prep TEXT, " +
                    "thumb TEXT, " +
                    "ingre TEXT, " +
                    "howto TEXT, " +
                    "info TEXT, " +
                    "snackId INTEGER)";
    db.execSQL(sql);

    ContentValues values = new ContentValues();

    values.put("name", "Name 1");
    values.put("disc", "here is the description");
    values.put("photo", "stub.png");
    values.put("thumb", "stub.png");
    values.put("prep", "takes 30 mins");
    values.put("ingre", "the ingredients of the snack");
    values.put("howto", "how to make this thing");
    values.put("info", "basically its this much calorie and such and such");
    db.insert("snacks", "name", values);

    values.put("name", "Name 2");
    values.put("disc", "here is the description");
    values.put("photo", "stub.png");
    values.put("thumb", "ic_launcher.png");
    values.put("prep", "takes 500 mins");
    values.put("ingre", "the ingredients of the snack");
    values.put("howto", "how to make this thing");
    values.put("info", "basically its this much calorie and such and such");
    db.insert("snacks", "name", values);

    values.put("name", "Name 3");
    values.put("disc", "here is the description");
    values.put("photo", "stub.png");
    values.put("thumb", "stub.png");
    values.put("ingre", "the ingredients of the snack");
    values.put("howto", "how to make this thing");
    values.put("info", "basically its this much calorie and such and such");
    db.insert("snacks", "name", values);

    values.put("name", "Name 4");
    values.put("disc", "here is the description");
    values.put("photo", "stub.png");
    values.put("ingre", "the ingredients of the snack");
    values.put("howto", "how to make this thing");
    values.put("info", "basically its this much calorie and such and such");
    db.insert("snacks", "name", values);

    values.put("name", "Name 5");
    values.put("disc", "here is the description");
    values.put("photo", "stub.png");
    values.put("ingre", "the ingredients of the snack");
    values.put("howto", "how to make this thing");
    values.put("info", "basically its this much calorie and such and such");
    db.insert("snacks", "name", values);

    values.put("name", "Name 6");
    values.put("disc", "here is the description");
    values.put("photo", "stub.png");
    values.put("ingre", "the ingredients of the snack");
    values.put("howto", "how to make this thing");
    values.put("info", "basically its this much calorie and such and such");
    db.insert("snacks", "name", values);

    values.put("name", "Name 7");
    values.put("disc", "here is the description");
    values.put("photo", "stub.png");
    values.put("ingre", "the ingredients of the snack");
    values.put("howto", "how to make this thing");
    values.put("info", "basically its this much calorie and such and such");
    db.insert("snacks", "name", values);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS snacks");
    onCreate(db);
}}

我似乎無法添加任何項目或編輯任何項目,而無需取消安裝應用程序然后重新安裝:(請幫助!!!

無需再次卸載和安裝應用程序,只需增加數據庫的版本號即可調用onUpgrade方法。

public Cook_tab_snacks_data(Context context) {
    super(context, DATABASE_NAME, null, 2);  //previously 1, now 2
}

就像在onUpgrade方法中一樣,首先它將刪除現有表,然后將調用onCreate方法來重新創建表

由於數據庫版本未更改,因此未調用您的onUpgrade函數。 如果每次更改數據庫時都增加版本號,則onUpgrade函數將刪除它並重新創建它。

超級構造函數的最后一個輸入參數是數據庫版本號:

public Cook_tab_snacks_data(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

僅在創建數據庫時才調用SQLiteOpenHelper的onCreate

如果要向數據庫添加值,則可以在Activity或Service中通過實例getWritableDatabase()類,調用getWritableDatabase()然后以與onCreate相同的方式調用insert來實現。

如果要更改架構,則需要在onUpgrade

如果需要一些練習, 記事本示例是學習在應用程序中使用SQLiteDatabase的好地方。

您也有一個問題,因為該數據庫僅創建一次,並且如果您要更改其任何內容,則必須將其卸載。

這就是事情的運作方式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM