简体   繁体   English

更新SQLite数据库Android

[英]Updating SQLite database android

Okay, I made a database and i realised that each time i change something in the database i have to uninstall then reinstall the Application :( which is very frustrating... here is my code for my data base hopefully you can help me ! i dont know whats wrong with my code: 好的,我创建了一个数据库,我意识到每次更改数据库中的某些内容时都必须先卸载然后重新安装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);
}}

I can't seem to add any item , or edit any item without having to unistall the application then reinstalling :( HELP PLEASE!!! thanks alot! 我似乎无法添加任何项目或编辑任何项目,而无需取消安装应用程序然后重新安装:(请帮助!!!

Instead of uninstalling and installing the application again, simply increment the version number of your database to call onUpgrade method. 无需再次卸载和安装应用程序,只需增加数据库的版本号即可调用onUpgrade方法。

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

as in your onUpgrade method, first it will delete the existing table and then will call onCreate method to recreate the table(s) 就像在onUpgrade方法中一样,首先它将删除现有表,然后将调用onCreate方法来重新创建表

Your onUpgrade function is not getting called because the version of the database is not changing. 由于数据库版本未更改,因此未调用您的onUpgrade函数。 If you increment the version number every time you make a change to the database, the onUpgrade function will drop it and recreate it. 如果每次更改数据库时都增加版本号,则onUpgrade函数将删除它并重新创建它。

The last input parameter to the super constructor is the database version number : 超级构造函数的最后一个输入参数是数据库版本号:

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

onCreate of the SQLiteOpenHelper is called only when the database is created. 仅在创建数据库时才调用SQLiteOpenHelper的onCreate

If you want to add values to the database, you can do so in an Activity or Service by instantiating your subclass, calling getWritableDatabase() and then calling insert in the same way you are doing in onCreate . 如果要向数据库添加值,则可以在Activity或Service中通过实例getWritableDatabase()类,调用getWritableDatabase()然后以与onCreate相同的方式调用insert来实现。

If you want to change the schema, you need to do that in onUpgrade . 如果要更改架构,则需要在onUpgrade

The notepad example is a good place to learn about using a SQLiteDatabase in an app if you need some practice. 如果需要一些练习, 记事本示例是学习在应用程序中使用SQLiteDatabase的好地方。

You have too because the database is created just once and if you want to change anything about it you have to uninstall it. 您也有一个问题,因为该数据库仅创建一次,并且如果您要更改其任何内容,则必须将其卸载。

this is the way things works 这就是事情的运作方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM