Last active
September 20, 2023 11:12
-
-
Save firatkarababa/d9472e7f8388c9fa7b6e745eb704fe43 to your computer and use it in GitHub Desktop.
Copy pre-populated SQLite database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.content.Context; | |
| import android.util.Log; | |
| import com.karababapps.ferhengvariants.database.AppDatabase; | |
| import java.io.File; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.OutputStream; | |
| import androidx.room.Room; | |
| public class DatabaseCopier { | |
| private static final String TAG = DatabaseCopier.class.getSimpleName(); | |
| private static final String DATABASE_NAME = "dictionaryWords.db"; | |
| private AppDatabase mAppDataBase; | |
| private static Context appContext; | |
| private static class Holder { | |
| private static final DatabaseCopier INSTANCE = new DatabaseCopier(); | |
| } | |
| public static DatabaseCopier getInstance(Context context) { | |
| appContext = context; | |
| return Holder.INSTANCE; | |
| } | |
| private DatabaseCopier() { | |
| //call method that check if database not exists and copy prepopulated file from assets | |
| copyAttachedDatabase(appContext, DATABASE_NAME); | |
| mAppDataBase = Room.databaseBuilder(appContext, | |
| AppDatabase.class, DATABASE_NAME) | |
| .addMigrations(AppDatabase.MIGRATION_1_2) | |
| .build(); | |
| } | |
| public AppDatabase getRoomDatabase() { | |
| return mAppDataBase; | |
| } | |
| private void copyAttachedDatabase(Context context, String databaseName) { | |
| final File dbPath = context.getDatabasePath(databaseName); | |
| // If the database already exists, return | |
| if (dbPath.exists()) { | |
| return; | |
| } | |
| // Make sure we have a path to the file | |
| dbPath.getParentFile().mkdirs(); | |
| // Try to copy database file | |
| try { | |
| final InputStream inputStream = context.getAssets().open("databases/" + databaseName); | |
| final OutputStream output = new FileOutputStream(dbPath); | |
| byte[] buffer = new byte[8192]; | |
| int length; | |
| while ((length = inputStream.read(buffer, 0, 8192)) > 0) { | |
| output.write(buffer, 0, length); | |
| } | |
| output.flush(); | |
| output.close(); | |
| inputStream.close(); | |
| } | |
| catch (IOException e) { | |
| Log.d(TAG, "Failed to open file", e); | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey..
This is such a nice Code DatabaseCopier
i am trying to use this with my Project, would you to help me for that
i unable to find this import
import com.karababapps.ferhengvariants.database.AppDatabase;
Thanks.