繁体   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