Advertisement
tafazzi87

MyOpenHelper

Jun 20th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. package com.example.provadatabase;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.sql.SQLException;
  8.  
  9. import android.content.Context;
  10. import android.database.sqlite.SQLiteDatabase;
  11. import android.database.sqlite.SQLiteException;
  12. import android.database.sqlite.SQLiteOpenHelper;
  13.  
  14. public class MyOpenHelper extends SQLiteOpenHelper{
  15.          
  16.         //The Android's default system path of your application database.
  17.         private static String DB_PATH = "/data/data/com.example.provadatabase/databases/";
  18.      
  19.         private static String DB_NAME = "orari";
  20.      
  21.         private SQLiteDatabase myDataBase;
  22.      
  23.         private final Context myContext;
  24.      
  25.         /**
  26.          * Constructor
  27.          * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
  28.          * @param context
  29.          */
  30.         public MyOpenHelper(Context context) {
  31.      
  32.             super(context, DB_NAME, null, 1);
  33.             this.myContext = context;
  34.         }  
  35.      
  36.       /**
  37.          * Creates a empty database on the system and rewrites it with your own database.
  38.          * */
  39.         public void createDataBase() throws IOException{
  40.      
  41.             boolean dbExist = checkDataBase();
  42.      
  43.             if(dbExist){
  44.                 //do nothing - database already exist
  45.             }else{
  46.      
  47.                 //By calling this method and empty database will be created into the default system path
  48.                    //of your application so we are gonna be able to overwrite that database with our database.
  49.                 this.getReadableDatabase();
  50.      
  51.                 try {
  52.      
  53.                     copyDataBase();
  54.      
  55.                 } catch (IOException e) {
  56.      
  57.                     throw new Error("Error copying database");
  58.      
  59.                 }
  60.             }
  61.      
  62.         }
  63.      
  64.         /**
  65.          * Check if the database already exist to avoid re-copying the file each time you open the application.
  66.          * @return true if it exists, false if it doesn't
  67.          */
  68.         private boolean checkDataBase(){
  69.      
  70.             SQLiteDatabase checkDB = null;
  71.      
  72.             try{
  73.                 String myPath = DB_PATH + DB_NAME;
  74.                 checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  75.      
  76.             }catch(SQLiteException e){
  77.      
  78.                 //database does't exist yet.
  79.      
  80.             }
  81.      
  82.             if(checkDB != null){
  83.      
  84.                 checkDB.close();
  85.      
  86.             }
  87.      
  88.             return checkDB != null ? true : false;
  89.         }
  90.      
  91.         /**
  92.          * Copies your database from your local assets-folder to the just created empty database in the
  93.          * system folder, from where it can be accessed and handled.
  94.          * This is done by transfering bytestream.
  95.          * */
  96.         private void copyDataBase() throws IOException{
  97.      
  98.             //Open your local db as the input stream
  99.             InputStream myInput = myContext.getAssets().open(DB_NAME);
  100.      
  101.             // Path to the just created empty db
  102.             String outFileName = DB_PATH + DB_NAME;
  103.      
  104.             //Open the empty db as the output stream
  105.             OutputStream myOutput = new FileOutputStream(outFileName);
  106.      
  107.             //transfer bytes from the inputfile to the outputfile
  108.             byte[] buffer = new byte[1024];
  109.             int length;
  110.             while ((length = myInput.read(buffer))>0){
  111.                 myOutput.write(buffer, 0, length);
  112.             }
  113.      
  114.             //Close the streams
  115.             myOutput.flush();
  116.             myOutput.close();
  117.             myInput.close();
  118.      
  119.         }
  120.      
  121.         public void openDataBase() throws SQLException{
  122.      
  123.             //Open the database
  124.             String myPath = DB_PATH + DB_NAME;
  125.             myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  126.      
  127.         }
  128.      
  129.         @Override
  130.         public synchronized void close() {
  131.      
  132.                 if(myDataBase != null)
  133.                     myDataBase.close();
  134.      
  135.                 super.close();
  136.      
  137.         }
  138.         @Override
  139.         public void onCreate(SQLiteDatabase db) {
  140.             // TODO Auto-generated method stub
  141.            
  142.         }
  143.  
  144.         @Override
  145.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  146.             // TODO Auto-generated method stub
  147.            
  148.         }
  149.      
  150.             // Add your public helper methods to access and get content from the database.
  151.            // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
  152.            // to you to create adapters for your views.
  153.      
  154.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement