Advertisement
ConorB4

Untitled

Oct 17th, 2015
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. // package goes here
  2.  
  3. import android.content.Context;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteException;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. import android.util.Log;
  8.  
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.OutputStream;
  13. import java.util.Locale;
  14.  
  15. public class DatabaseHelper extends SQLiteOpenHelper {
  16.  
  17.     private static final String DB_PATH = "PATH";
  18.  
  19.     private static final String DB_NAME = "DB";
  20.  
  21.     private final Context myContext;
  22.  
  23.     public SQLiteDatabase db;
  24.  
  25.     public DatabaseHelper(Context context) {
  26.         super(context, DB_NAME, null, 1);
  27.         this.myContext = context;
  28.     }
  29.  
  30.     @Override
  31.     public void onCreate(SQLiteDatabase db) {
  32.     }
  33.  
  34.     @Override
  35.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  36.     }
  37.  
  38.     public void createDatabase() {
  39.         createDB();
  40.     }
  41.  
  42.     private void createDB() {
  43.         boolean dbExist = DBExists();
  44.  
  45.         if(!dbExist) {
  46.             this.getReadableDatabase();
  47.             copyDBFromResource();
  48.         }
  49.     }
  50.  
  51.     private boolean DBExists() {
  52.         SQLiteDatabase db = null;
  53.  
  54.         try {
  55.             String databasePath = DB_PATH + DB_NAME;
  56.             db = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
  57.             db.setLocale(Locale.getDefault());
  58.             db.setLockingEnabled(true);
  59.             db.setVersion(1);
  60.         } catch (SQLiteException e) {
  61.             Log.e("SqlHelper", "database not found");
  62.         }
  63.  
  64.         if (db != null) {
  65.             db.close();
  66.         }
  67.         return db != null ? true : false;
  68.     }
  69.  
  70.     private void copyDBFromResource() {
  71.         InputStream inputStream = null;
  72.         OutputStream outStream = null;
  73.         String dbFilePath = DB_PATH + DB_NAME;
  74.  
  75.         try {
  76.             inputStream = myContext.getAssets().open(DB_NAME);
  77.             outStream = new FileOutputStream(dbFilePath);
  78.  
  79.             byte[] buffer = new byte[1024];
  80.             int length;
  81.             while ((length = inputStream.read(buffer)) > 0) {
  82.                 outStream.write(buffer, 0, length);
  83.             }
  84.  
  85.             outStream.flush();
  86.             outStream.close();
  87.             inputStream.close();
  88.  
  89.         } catch (IOException e) {
  90.             throw new Error("Problem copying database from resource file.");
  91.         }
  92.  
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement