首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android ADB将测试SQLite模式更新

Android ADB将测试SQLite模式更新
EN

Stack Overflow用户
提问于 2012-10-04 05:04:49
回答 1查看 502关注 0票数 1

更新:解决方案=修复了onUpgrade()代码,该代码允许使用调试器成功测试数据库架构更改,而不是打包多个apks

我正在尝试测试2个app,以确保数据库更新将发生时,我的应用程序更新部署到市场。

:包含sqlite schema_version =1 Test2.apk:包含sqlite schema_version =2

这两个apks都是用相同的密钥进行数字签名的,使用adb我可以安装Test1.apk。当我尝试安装Test2.apk时,我收到:

故障INSTALL_FAILED_ALREADY_EXISTS

如何测试我的新架构更改?如果答案来自调试器,那么当调用onUpgrade()时,我的数据库总是锁定的。

代码语言:javascript
运行
复制
//////////////// This Code Fixed The onUpgrade() Issue /////////////////////

@Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
            //TODO: Write DB Update logic.

            SQLiteDatabase upgradeDB = null;

            if (newVersion > oldVersion){           
                Log.e(TAG, "NEWER VERSION DETECTED, DATABASE UPGRADE REQUIRED!");               

                InputStream inputStream = myContext.getResources().openRawResource(R.raw.dbscript_v2_0_0_0); 
                BufferedReader br2 = new BufferedReader(new InputStreamReader(inputStream), 1016688); //1016688 max 
                String buffer;
                try {

                    db.beginTransaction();

                    //openDataBase();
                    while ((buffer = br2.readLine()) != null) 
                    { 
                            String[] execSql = buffer.split("\n");  
                            execMultipleSQL(db, execSql);

                    } 
                    db.setTransactionSuccessful();      

                    Log.d(TAG, "onCreated sql: CREATED TABLES and INSERTED RECORDS");
                } catch (SQLException e) {
                    Log.e("Error creating tables and debug data", e.toString());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    db.endTransaction();
                    //db.close();
                }

            }
            else
            {
                Log.e(TAG, "NO DATABASE UPGRADE DETECTED"); 
            }
        }

////////////////////////////////////////////////////////////////////////////
代码语言:javascript
运行
复制
        @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
                    //TODO: Write DB Update logic.

                    SQLiteDatabase upgradeDB = null;

                    if (newVersion > oldVersion){           
                        Log.e(TAG, "NEWER VERSION DETECTED, DATABASE UPGRADE REQUIRED!");               

                        InputStream inputStream = myContext.getResources().openRawResource(R.raw.dbscript_v2_0_0_0); 
                        BufferedReader br2 = new BufferedReader(new InputStreamReader(inputStream), 1016688); //1016688 max 
                        String buffer;
                        try {

                            String myPath = DATABASE_PATH + DATABASE_NAME;
                            myDataBase = SQLiteDatabase.openOrCreateDatabase(DATABASE_PATH + DATABASE_NAME, null);
                            myDataBase.beginTransaction();

                            //openDataBase();
                            while ((buffer = br2.readLine()) != null) 
                            { 
                                    String[] execSql = buffer.split("\n");  
                                    execMultipleSQL(myDataBase, execSql);

                            } 
                            myDataBase.setTransactionSuccessful();      
                                //close();                  
                                Log.d(TAG, "onCreated sql: CREATED TABLES and INSERTED RECORDS");
                        } catch (SQLException e) {
                            Log.e("Error creating tables and debug data", e.toString());
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } finally {
                            myDataBase.endTransaction();
                            myDataBase.close();
                        }

                    }
                    else
                    {
                        Log.e(TAG, "NO DATABASE UPGRADE DETECTED"); 
                    }
                }


     public void openDataBase() throws SQLException {

                try
                {   
                    /* When the database is 1st installed on the device, this helper class creates a blank database using getWritableDatabase().
                     * The actual database is then populated using the SQLiteDatabase.openDatabase(PATH, null, Open_ReadWrite) to exec the sql.
                     * 
                     * PROBLEM - SQLiteDatabase.openDatabase() Does not call onUpgrade() so we're 
                     * unable to update the database for future versions
                     * 
                     * SOLUTION - Check to see if the dbExists, if not use SQLiteDatabase.openDatabase() 
                     * because it will create the DB from scripts, we don't care about onUpgrade();
                     * If the db does exist, call getWritableDatabase() to invoke onUpgrade checks for 
                     * future releases where the SCHEMA_VERSION is greater than the current db SCHEMA_VERSION
                     */
                    boolean dbExist = checkDataBase();
                    if(!dbExist){

                        /* Calls: DOES NOT call onUpgrade()
                         * Errors if db exists and there are db changes (New Columns/Tables), so use the below getWritableDatabase() instead                     */
                        String myPath = DATABASE_PATH + DATABASE_NAME;
                        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
                    }
                    else
                    {
                        /* Calls: onUpgrade()
                         * Errors if db does not exist, so use the above instead                     */
                        myDataBase = this.getWritableDatabase();                    
                    }               

                }
                catch (Exception e) {
                    Log.e(TAG, "Error SQLiteFactoryAdapter openDataBase " + e.toString());
                }
            }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-04 05:12:58

你是如何打开数据库的?

看起来你在扩展SQLiteOpenHelper。构造函数有一个参数version,它表示架构的版本。为什么不在测试中更改这个参数来测试onUpgrade()方法呢

附注:您收到错误消息的原因是因为您正在尝试再次部署相同的包。您需要先卸载它,然后再重新部署它。您可以使用命令adb uninstall packagename.apk来完成此操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12716885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档