java - 更改项目包名称后数据库出现问题

标签 java android sqlite exception

在更改项目的包名称后,数据库出现问题,应用程序运行,但在 logcat 中,我看到多个 DeadObjectExceptions,这些异常在包名称更改之前没有发生。这是数据库类

package com.posks.lernedeutsch.dieartikel.db;

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.List;

    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteException;
    import android.database.sqlite.SQLiteOpenHelper;

    import com.posks.lernedeutsch.dieartikel.quiz.Question;


    public class DBHelper extends SQLiteOpenHelper{

        //The Android's default system path of your application database.
        private static String DB_PATH = "/data/data/com.posks.lernedeutsch.dieartikel/databases/";
        private static String DB_NAME = "questionsDb";
        private SQLiteDatabase myDataBase; 
        private final Context myContext;

        /**
         * Constructor
         * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
         * @param context
         */
        public DBHelper(Context context) {
            super(context, DB_NAME, null, 1);
            this.myContext = context;
        }   

        /**
         * Creates a empty database on the system and rewrites it with your own database.
         * */
        public void createDataBase() throws IOException{

            boolean dbExist = checkDataBase();
            if(!dbExist)
            {
                //By calling this method and empty database will be created into the default system path
                //of your application so we are gonna be able to overwrite that database with our database.
                this.getReadableDatabase();

                try {
                    copyDataBase(); 
                } catch (IOException e) {
                    throw new Error("Fehler beim Kopieren der Datenbank");
                }
            }
        }

        /**
         * Check if the database already exist to avoid re-copying the file each time you open the application.
         * @return true if it exists, false if it doesn't
         */
        private boolean checkDataBase(){
            SQLiteDatabase checkDB = null;
            try{
                String myPath = DB_PATH + DB_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
            }catch(SQLiteException e){
                //database does't exist yet.
            }
            if(checkDB != null){
                checkDB.close();
            }

            return checkDB != null ? true : false;
        }

        /**
         * Copies your database from your local assets-folder to the just created empty database in the
         * system folder, from where it can be accessed and handled.
         * This is done by transfering bytestream.
         * */
        private void copyDataBase() throws IOException{

            //Open your local db as the input stream
            InputStream myInput = myContext.getAssets().open(DB_NAME);

            // Path to the just created empty db
            String outFileName = DB_PATH + DB_NAME;

            //Open the empty db as the output stream
            OutputStream myOutput = new FileOutputStream(outFileName);

            //transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer))>0){
                myOutput.write(buffer, 0, length);
            }

            //Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();

        }

        public void openDataBase() throws SQLException{
            //Open the database
            String myPath = DB_PATH + DB_NAME;
            myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        }

        @Override
        public synchronized void close() {
            if(myDataBase != null)
                myDataBase.close();
            super.close();
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }

        // Add your public helper methods to access and get content from the database.
        // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
        // to you to create adapters for your views.




        public List<Question> getQuestionSet(int difficulty, int numQ){
            List<Question> questionSet = new ArrayList<Question>();
            Cursor c = myDataBase.rawQuery("SELECT * FROM QUESTIONS WHERE DIFFICULTY=" + difficulty +
                    " ORDER BY RANDOM() LIMIT " + numQ, null);
            while (c.moveToNext()){
                //Log.d("QUESTION", "Question Found in DB: " + c.getString(1));
                Question q = new Question();
                q.setQuestion(c.getString(1));
                q.setAnswer(c.getString(2));
                q.setOption1(c.getString(3));
                q.setOption2(c.getString(4));
                q.setOption3(c.getString(5));
                q.setRating(difficulty);
                questionSet.a`enter code here`dd(q);
            }
            return questionSet;
        }
    }

最佳答案

数据库仅在新安装时复制。我将从测试设备/虚拟机中卸载该应用程序,以确保它正确复制回来。如果问题仍然存在,我会搜索您的项目以确保没有任何其他对旧包名称的引用,因为重构时可能并非每个引用都发生了更改。

关于java - 更改项目包名称后数据库出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26744338/

相关文章:

java - 避免 XML 注入(inject)攻击的 DOM 解析器设置

java - 区分 json 中字段不存在的情况和其值作为 null 发送的情况

java - Collection 扩展了 Iterable,但为什么 Collection 声明 Iterable Function(与 Iterable 相同)

java - 使用扩展日历 View 显示事件信息

java - 如何在计数器应用程序中继续从共享首选项编号开始计数

java - 截屏并通过 Selenium WebDriver 和 java 中的 FileUtils 进行复制

java - OpenGL形状绘图绕y轴反转?

php - 如何检测 Android 应用程序的卸载并从服务器中删除相关用户的数据?

android - 如何在 SQLite 中迭代文本字符串?

java - 更新 SQLite 数据库中的整数值 (java)