Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use existing sqlite database in android

    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.51k
    Comment on it

    I will show you the easiest solution, by far, is to use SQLiteAssetHelper. You add your existing sqlite database in a specified location in your project's assets/ directory, then use SQLiteAssetHelper to access your database (much same as you would use SQLiteOpenHelper).

    This is the DataBaseHelper class to copy and open your sqlite file from the asset folder.

    1. public class DataBaseHelper extends SQLiteOpenHelper{
    2. private static String DB_PATH = "/data/data/YOUR PAKAGE NAME/databases/";
    3. private static String DB_NAME = "YOUR DB NAME";
    4. public SQLiteDatabase myDataBase;
    5. private static Context myContext;
    6. /*
    7. * Constructor
    8. * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
    9. * @param context
    10. */
    11. public DataBaseHelper(Context context) {
    12. super(context, DB_NAME, null, 1);
    13. myContext = context;
    14. }
    15. /*
    16. * Creates a empty database on the system and rewrites it with your own database.
    17. * */
    18. public void createDataBase() throws IOException{
    19. boolean dbExist = checkDataBase();
    20. if(dbExist)
    21. {
    22. System.out.println("Database already exist");
    23. }
    24. else
    25. {
    26. //By calling this method and empty database will be created into the default system path
    27. //of your application so we are gonna be able to overwrite that database with our database.
    28. this.getReadableDatabase();
    29. try
    30. {
    31. copyDataBase();
    32. }
    33. catch (IOException e)
    34. {
    35. System.out.println("Error copying data"+e.toString()+e.getMessage());
    36. }
    37. }
    38. }

    39.  
    40. /**

    41. * Check if the database already exist to avoid re-copying the file each time you open the application.
    42. * @return true if it exists, false if it doesn't
    43. */
    44. private boolean checkDataBase()
    45. {
    46. SQLiteDatabase checkDB = null;
    47. try
    48. {
    49. String myPath = DB_PATH + DB_NAME;
    50. checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    51. System.out.println("checkdb::"+checkDB);
    52. }
    53. catch(SQLiteException e)
    54. {

    55.  
    56.     System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+e.toString());
    57.  
    58. }
    59. if(checkDB != null)
    60. {
    61. System.out.println("DB found");
    62. checkDB.close();
    63. }
    64. else
    65. {
    66. System.out.println("DB not found");
    67. }
    68. return checkDB != null ? true : false;
    69.  
    70.  
    71. }

    72.  
    73. /**

    74. * Copies your database from your local assets-folder to the just created empty database in the
    75. * system folder, from where it can be accessed and handled.
    76. * This is done by transfering bytestream.
    77. * */
    78. private void copyDataBase() throws IOException
    79. {
    80. System.out.println("Working 1");
    81. //Open your local db as the input stream
    82. InputStream myInput = myContext.getAssets().open(DB_NAME);
    83. // Path to the just created empty db
    84. String outFileName = DB_PATH + DB_NAME;
    85. //Open the empty db as the output stream
    86. OutputStream myOutput = new FileOutputStream(outFileName);
    87. //transfer bytes from the inputfile to the outputfile
    88. byte[] buffer = new byte[1024];
    89. int length;
    90. while ((length = myInput.read(buffer))>0)
    91. {
    92. myOutput.write(buffer, 0, length);
    93. }

    94.  
    95. //Close the streams
    96. myOutput.flush();
    97. myOutput.close();
    98. myInput.close();
    99.  
    100.  
    101. }

    102. public void openDataBase() throws SQLException{

    103.  
    104. //Open the database     
    105. String myPath = DB_PATH + DB_NAME;
    106. System.out.println("DBpath::"+myPath);
    107. //myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    108. myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    109. System.out.println("success");
    110.  
    111.  
    112. }

    113.  
    114. @Override

    115. public synchronized void close() {

    116.  
    117. if(myDataBase != null)
    118. myDataBase.close();
    119.  
    120. super.close();
    121.  
    122.  
    123. }

    124.  
    125. @Override

    126. public void onCreate(SQLiteDatabase db) {
    127. // TODO Auto-generated method stub

    128.  
    129. }

    130.  
    131. @Override

    132. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    133. // TODO Auto-generated method stub

    134.  
    135. }

    136. }

    Now you just have to call this class in the activity where you want to open this database.

    just like this

    1. DataBaseHelper dataBaseHelper=new DataBaseHelper(this);
    2. try
    3. {
    4. dataBaseHelper.createDataBase();
    5. dataBaseHelper.openDataBase();
    6. }
    7. catch (IOException e)
    8. {
    9. e.printStackTrace();
    10. }

    Enjoy:-

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: