Posted By

jamepaul on 09/10/11


Tagged

database java SQLite android


Versions (?)

Own sqlite database in android


 / Published in: Java
 

URL: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

  1. package com.package.name;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7.  
  8. import android.content.Context;
  9. import android.database.Cursor;
  10. import android.database.SQLException;
  11. import android.database.sqlite.SQLiteDatabase;
  12. import android.database.sqlite.SQLiteException;
  13. import android.database.sqlite.SQLiteOpenHelper;
  14.  
  15. public class DatabaseHelper extends SQLiteOpenHelper {
  16.  
  17. private static String DB_PATH = "/data/data/com.hotel.directory/databases/";
  18. private static String DB_NAME = "dbname.db";
  19. private SQLiteDatabase db;
  20. private final Context myContext;
  21.  
  22. public DatabaseHelper(Context context) {
  23. super(context, DB_NAME, null, 1);
  24. this.myContext = context;
  25. }
  26.  
  27. /**
  28. * Creates a empty database on the system and rewrites it with your own database.
  29. */
  30. public void createDatabase() throws IOException {
  31. boolean dbExist = checkDataBase();
  32.  
  33. if(dbExist) {
  34. //do nothing - database already exist
  35. } else {
  36. //By calling this method and empty database will be created into the default system path
  37. //of your application so we are gonna be able to overwrite that database with our database.
  38. this.getReadableDatabase();
  39.  
  40. try {
  41. copyDataBase();
  42. } catch(IOException e) {
  43. throw new Error("Error copying database");
  44. }
  45. }
  46. }
  47.  
  48. /**
  49. * Check if the database already exist to avoid re-copying the file each time you open the application.
  50. * @return true if it exists, false if it dosen't
  51. */
  52. private boolean checkDataBase(){
  53. SQLiteDatabase checkDB = null;
  54.  
  55. try {
  56. String myPath = DB_PATH + DB_NAME;
  57. checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  58. } catch (SQLiteException e) {
  59.  
  60. }
  61.  
  62. if(checkDB != null) {
  63. checkDB.close();
  64. }
  65.  
  66. return checkDB != null ? true : false;
  67. }
  68.  
  69. /**
  70. * Copies your database from your local assets-folder to the just created empty database in the
  71. * system folder, from where it can be accessed and handled.
  72. * This is done by transfering bytestream.
  73. */
  74. private void copyDataBase() throws IOException {
  75. //Open your local db as the input stream
  76. InputStream myInput = myContext.getAssets().open(DB_NAME);
  77.  
  78. // Path to the just created empty db
  79. String outFileName = DB_PATH + DB_NAME;
  80.  
  81. // Open the empty db as the output stream
  82. OutputStream myOutput = new FileOutputStream(outFileName);
  83.  
  84. //transfer bytes from the inputfile to the outputfile
  85. byte[] buffer = new byte[1024];
  86. int length;
  87. while((length = myInput.read(buffer))>0) {
  88. myOutput.write(buffer, 0, length);
  89. }
  90.  
  91. // Close the streams
  92. myOutput.flush();
  93. myOutput.close();
  94. myInput.close();
  95. }
  96.  
  97. public void openDataBase() throws SQLException {
  98. //Open the database
  99. String myPath = DB_PATH + DB_NAME;
  100. db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  101. }
  102.  
  103. @Override
  104. public synchronized void close() {
  105. if(db != null)
  106. db.close();
  107.  
  108. super.close();
  109. }
  110.  
  111. @Override
  112. public void onCreate(SQLiteDatabase db) {
  113. // TODO Auto-generated method stub
  114.  
  115. }
  116.  
  117. @Override
  118. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  119. // TODO Auto-generated method stub
  120.  
  121. }
  122.  
  123. }

Report this snippet  

You need to login to post a comment.