如何更新已经安装的应用数据库的变化 [英] How to Update Database changes to already Installed Application

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

问题描述

如果让我喜欢在我的Andr​​oid应用程序添加新列的任何更改数据库。
如果我已经安装了我的手机上。
当我重新安装/更新该应用程序在移动它不会更新数据库的变化。

我的数据库code是:

 包com.screenmagic.smsmagicmobileapp;进口java.text.SimpleDateFormat的;
进口java.util.Date;进口android.content.ContentValues​​;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.util.Log;
公共类数据库处理器扩展SQLiteOpenHelper {
私有静态最终诠释DATABASE_VERSION = 1;
私有静态最后弦乐DATABASE_NAME =测试;
公共数据库处理器(上下文的背景下){
    超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
}
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
    Log.d(TAG,数据库处理器的的onCreate);    db.execSQL(DROP TABLE IF EXISTS史);
    字符串CREATE_SMS_HISTORY_TABLE =CREATE TABLE历史(+
    ID INTEGER PRIMARY KEY AUTOINCREMENT,移动电话号码TEXT,smsText文本);
    Log.d(插入,表创建查询::+ CREATE_SMS_HISTORY_TABLE);
    db.execSQL(CREATE_SMS_HISTORY_TABLE);
    Log.d(插入,表格创建..);}//数据库升级
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
    db.execSQL(DROP TABLE IF EXISTS sms_history);
    的onCreate(DB);
}
}


解决方案

只需使用onUpgrade(作为一个覆盖)方法,你可以把你的数据库升级那里。这样一来,已安装应用程序时没有任何现有的数据将被吹走。

和确保你不降,你目前工作作为表:

  db.execSQL(DROP TABLE IF EXISTS sms_history);

请参阅该方法参考<一个href=\"http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#onUpgrade%28android.database.sqlite.SQLiteDatabase,%20int,%20int%29\"相对=nofollow>此处以及如何实现它的视频这里

If I make any change to database like adding new column in my android application. and If I have already installed that on my phone. when I reinstall / update that application on mobile it doesn't update the database changes.

My database code is :

package com.screenmagic.smsmagicmobileapp;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DatabaseHandler extends SQLiteOpenHelper {


private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Test";


public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    Log.d(TAG, "onCreate OF DatabaseHandler");

    db.execSQL("DROP TABLE IF EXISTS history");
    String CREATE_SMS_HISTORY_TABLE = "CREATE TABLE  history("+
    "id INTEGER PRIMARY KEY AUTOINCREMENT, mobileNumber TEXT, smsText TEXT)";
    Log.d("Insert: ", "Table create query:: "+ CREATE_SMS_HISTORY_TABLE);
    db.execSQL(CREATE_SMS_HISTORY_TABLE);
    Log.d("Insert: ", "Table created ..");

}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS sms_history");
    onCreate(db);
}


}

解决方案

Just use the onUpgrade (as an override) method, and you can put your database upgrades there. this way, none of the existing data will be blown away when the application has already been installed.

And make sure you don't drop the table as you're currently doing on:

db.execSQL("DROP TABLE IF EXISTS sms_history");

See reference for the method here and a video on how to implement it here

这篇关于如何更新已经安装的应用数据库的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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