I have one android application which have sqllite database located in assets folder. I have published application in playstore with database version= null, than database version=1, database version=2 and now want publish application with database version=3. I have condition for remove last data and copy new data on update. its working if user have application installed with version 2, if user have version 1 database than its getting unfortunately stopped on main activity. What is problem in my condition for copy database ? My Database Helper class is like this. Thanks
public class DataBaseHandler extends SQLiteOpenHelper {
        private static String DB_PATH;
        private static String DB_NAME = "SuccessQuotesNew";
        private SQLiteDatabase myDataBase;
        private static int DATABASE_VERSION = 1;
        private final Context myContext;
        public DataBaseHandler(Context context) {
                super(context, DB_NAME, null, DATABASE_VERSION);
                this.myContext = context;
                DB_PATH = context.getDatabasePath(DB_NAME).toString();
                Log.e("path", DB_PATH);
        }
        public void createDataBase() throws IOException {
                boolean dbExist = checkDataBase();
                if(dbExist)
                {
                if(DATABASE_VERSION == 2)
                {
                        
                        try {
                                copyDataBase();
                                DATABASE_VERSION = 3;
                        } catch (IOException e) {
                                throw new Error("Error copying database");
                        }
                        
                }else
                {
                SQLiteDatabase database = null;
                database = this.getWritableDatabase();
                String query_count = "SELECT version FROM users";
                                
                Cursor c_count = database.rawQuery(query_count, null);
                
                
                c_count.moveToFirst();
                Integer count = c_count.getInt(c_count.getColumnIndex("version"));
        if(count == DATABASE_VERSION)
        {
                
        }else
        {
                this.getReadableDatabase();
                try {
                        copyDataBase();
                } catch (IOException e) {
                        throw new Error("Error copying database");
                }
        }
        }
                }else
                {
                        this.getReadableDatabase();
                        try {
                                copyDataBase();
                                DATABASE_VERSION  = 2;
                        } catch (IOException e) {
                                throw new Error("Error copying database");
                        }
                }
                        
        }
        private boolean checkDataBase() {
                SQLiteDatabase checkDB = null;
                try {
                        String myPath = DB_PATH;
                        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;
        }
        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;
                // 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;
                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) {
                
                
        }
}
Aucun commentaire:
Enregistrer un commentaire