简体   繁体   English

使用onUpgrade()更新预填充的SQLite数据库

[英]Updating my pre-populated SQLite database using onUpgrade()

I have been working on upgrading my pre populated SQLite database and cannot quite work out how to get onUpgrade to work. 我一直在努力升级预先填充的SQLite数据库,但无法完全确定如何使onUpgrade正常工作。 Please see below my code to copy across the database. 请在下面查看我的代码以跨数据库复制。

// MAIN MENU ON GAME LOAD UP
DB_PATH = getDatabasePath(DataBaseHelper.DB_NAME).toString();
DataBaseHelper myDbHelper = new DataBaseHelper(this);
myDbHelper = new DataBaseHelper(this);
try
{
    myDbHelper.createDataBase();
}
catch (IOException ioe)     
{
    throw new Error("Unable to create database");
}
myDbHelper.close();

// HELPER CLASS
public class DataBaseHelper extends SQLiteOpenHelper 
{
    private static final int DATABASE_VERSION = 1;

    public DataBaseHelper(Context context) 
    {
        super(context, DB_NAME, null, DATABASE_VERSION);
        this.myContext = context;
    }

    public void createDataBase() throws IOException
    {
        boolean dbExist = databaseExist();
        if(dbExist)
        {
            //database already exist
        }
        else
        {
            try 
            {
                copyDataBase();
            } 
            catch (IOException e) 
            {
                throw new Error("Error copying database");
            }
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {}

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        switch (oldVersion)
        {
            case 1:
                db.execSQL("UPDATE MazeOnDraw SET posX=1, posY=2 WHERE rowid=5");
                break;
        }
    }
}

So I am not sure what I am doing wrong here however onUpgrade never seems to get called, I am incrementing the database version. 因此,我不确定在这里做错了什么,但是似乎从未调用过onUpgrade,而是在增加数据库版本。 Further to this can this process work with multiple databases as I may have 2 different databases (although this can be condensed into 1 if need be). 此外,此过程可以与多个数据库一起使用,因为我可能有2个不同的数据库(尽管如果需要可以将其压缩为1个)。

Finally, can the onUpgrade process be tested through ecplise debugging or must it be tested through the alpha versions on the developer console. 最后,可以通过ecplise调试来测试onUpgrade进程,还是必须通过开发者控制台上的Alpha版本对其进行测试。

Please chagne DB_vERSION values to 2.. so DDMS can identify app have newer version of databasee for upgrade.. 请将DB_vERSION的值更改为2.。以便DDMS可以识别应用程序具有较新版本的数据库以进行升级。

CHANGE 更改

private static final int DATABASE_VERSION = 2;

super(context, DB_NAME, null, DATABASE_VERSION );

in this statement last argument is database version.. set it to 2 for new updated APK. 在此语句中,最后一个参数是数据库版本。 新的更新的APK 设置为2 so existing user can have newly added table Or Column. 因此现有用户可以拥有新添加的表或列。

on updated apk onUpgrade will call. 在更新的APK onUpgrade上会调用。 and there you can copy database from asset. 在那里您可以从资产复制数据库。 but than exisiting user will have loss of data.. so best option is to add table dynamically in database. 但最好的选择是在数据库中动态添加表。

refer this answer 参考这个答案

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

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