2012-03-15 163 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的好地方,如果您需要一些练习。