2012-03-15 2 views
1

好吧,我做了一個數據庫,我意識到,每次我改變數據庫中的東西我必須卸載,然後重新安裝應用程序:(這是非常令人沮喪的... 這裏是我的代碼爲我的數據基地希望你能幫助我,我不知道什麼是錯我的代碼:更新SQLite數據庫android

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); 
}} 

我似乎無法添加任何物品,或編輯任何項目,而無需反安裝應用程序然後再重新安裝:(請幫助! !! 非常感謝!

+0

您可以使用SQLite編輯器程序來創建/編輯表格,然後將其保存並複製到資產文件夾中,您可以從中使用並將其重新保存到SDCard中(如果需要)。 – Demonick 2012-03-15 19:55:59

回答

2

不是卸載並重新安裝應用程序,只需增加數據庫的版本號碼呼叫onUpgrade方法。

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

在你onUpgrade方法,首先它會刪除現有的表,然後將調用的onCreate方法重新

+0

我明白了!非常感謝,所以每當我更新它時,我都會更改號碼。 – FirstLaw 2012-03-16 18:05:30

+0

是的......這就是它應該是 – waqaslam 2012-03-16 20:12:25

-3

你也有,因爲數據庫只創建一次,如果你想要t o改變任何事情,你必須卸載它。

這是事物的工作方式

+0

嘿,那裏,我選擇的答案解決了我的問題。 :)感謝您的幫助。歡呼聲 – FirstLaw 2012-03-16 18:14:49

1

你onUpgrade功能是沒有得到所謂的,因爲表(S)數據庫的版本沒有變化。如果每次更改數據庫時增加版本號,onUpgrade函數都會將其刪除並重新創建。

最後輸入參數的超構造是數據庫的版本號:

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

謝謝隊友,但我選擇了第一個得到的答案。 感謝您的幫助!乾杯 – FirstLaw 2012-03-16 18:15:35

0

的SQLiteOpenHelper的onCreate僅當創建數據庫調用。

如果你想要將其值添加到數據庫中,您可以在活動或服務通過實例化你的子類,調用getWritableDatabase()然後在你onCreate正在做同樣的方式調用insert這樣做。

如果你想改變模式,你需要在onUpgrade中這樣做。

notepad example是學習如何在應用中使用SQLiteDatabase的好地方,如果您需要一些練習。