【问题标题】:Updating SQLite database android更新 SQLite 数据库 android
【发布时间】:2012-03-15 19:52:04
【问题描述】:

好的,我创建了一个数据库,我意识到每次我更改数据库中的某些内容时,我都必须卸载然后重新安装应用程序:(这非常令人沮丧... 这是我的数据库代码,希望你能帮助我!我不知道我的代码有什么问题:

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

我似乎无法添加任何项目,或编辑任何项目,而无需卸载应用程序然后重新安装:(请帮助!!! 非常感谢!

【问题讨论】:

  • 您可以使用 SQLite 编辑器程序来创建/编辑表格,然后将其保存并复制到 assets 文件夹中,如果需要,您可以从中使用并将其重新保存到 SDCard。

标签: android database sqlite sql-update


【解决方案1】:

无需再次卸载和安装应用程序,只需增加数据库的版本号即可调用 onUpgrade 方法。

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

与您的 onUpgrade 方法一样,它首先会删除现有表,然后调用 onCreate 方法重新创建表

【讨论】:

  • 我明白了!非常感谢,所以每次我更新它时,我都会更改数字。
【解决方案2】:

您的 onUpgrade 函数没有被调用,因为数据库的版本没有改变。如果每次更改数据库时都增加版本号,onUpgrade 函数将删除它并重新创建它。

超级构造函数的最后一个输入参数是数据库版本号:

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

【讨论】:

  • 谢谢队友,但我选择了我首先得到的答案。感谢你的帮助!欢呼
【解决方案3】:

SQLiteOpenHelper 的onCreate 仅在创建数据库时调用。

如果您想向数据库添加值,您可以在 Activity 或 Service 中通过实例化您的子类来执行此操作,调用 getWritableDatabase(),然后调用 insert,就像在 onCreate 中所做的一样。

如果您想更改架构,您需要在 onUpgrade 中进行。

如果您需要一些练习,notepad example 是了解如何在应用中使用 SQLiteDatabase 的好地方。

【讨论】:

    【解决方案4】:

    你也有,因为数据库只创建一次,如果你想更改它的任何内容,你必须卸载它。

    这就是事情的运作方式

    【讨论】:

    • 嘿,我选择的答案解决了我的问题。 :) 感谢您的帮助。欢呼
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多