更新SQLite数据库的android [英] Updating SQLite database android

查看:177
本文介绍了更新SQLite数据库的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我做了一个数据库,我意识到,我每次更改数据库中我必须卸载,然后重新安装应用程序:(这是非常令人沮丧的事情...
这里是我的code对我的数据的基础上希望你能帮帮我!我不知道什么是错我的code:

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:

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!

推荐答案

而不是卸载并重新安装应用程序,只需增加数据库的版本号码呼叫 onUpgrade 方式。

Instead of uninstalling and installing the application again, simply increment the version number of your database to call onUpgrade method.

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

在您的 onUpgrade 方式,首先它会删除现有的表,然后将调用的onCreate 方式来重新创建表(S)

as in your onUpgrade method, first it will delete the existing table and then will call onCreate method to recreate the table(s)

这篇关于更新SQLite数据库的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆