Advertisement
Guest User

DatabaseManager

a guest
Jul 12th, 2011
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.93 KB | None | 0 0
  1. // all import
  2.  
  3. public class DataBaseHelper extends SQLiteOpenHelper
  4. {
  5.  
  6.   // The Android's default system path of your application database.
  7.   private static final String DB_DEFAULT_FILE = "split-";
  8.   private static String DB_PATH = "/data/data/com.cfdict.android/databases/";
  9.   private static String DB_NAME = "cfdict.db";
  10.   private SQLiteDatabase db;
  11.   private static String SPLIT_FILES_PATH = "/assets/splitDb/";
  12.   private static final String TAG = "CFDICT";
  13.  
  14.   private final Context mContext;
  15.  
  16.   /**
  17.    * Constructor Takes and keeps a reference of the passed context in order to
  18.    * access to the application assets and resources.
  19.    *
  20.    * @param context
  21.    */
  22.   public DataBaseHelper(Context context)
  23.   {
  24.     super(context, DB_NAME, null, 1);
  25.     this.mContext = context;
  26.   }
  27.  
  28.   /**
  29.    * Creates a empty database on the system and rewrites it with your own
  30.    * database.
  31.    * */
  32.   public void createDataBase() throws IOException
  33.   {
  34.     boolean dbExist = checkDataBase();
  35.  
  36.     if (dbExist)
  37.     {
  38.       // do nothing - database already exist
  39.       Log.i(TAG, "createDataBase(): Database already exists");
  40.     } else
  41.     {
  42.       try
  43.       {
  44.         // By calling this method an empty database will be created into the
  45.         // default system path of your application so we are gonna be able to
  46.         // overwrite that database with our database.
  47.         Log.i(TAG, "createDataBase(): Create an empty database");
  48.         this.getReadableDatabase();
  49.       } catch (SQLiteException sqliteE)
  50.       {
  51.         sqliteE.printStackTrace();
  52.         Log.e(TAG, "Error database cannot be opened");
  53.         throw new Error("Error database cannot be opened");
  54.       }
  55.  
  56.       try
  57.       {
  58.         Log.i(TAG, "createDataBase(): Copy database…");
  59.         copyDataBase();
  60.       } catch (IOException ioE)
  61.       {
  62.         Log.e(TAG, "createDataBase(): FAIL to copy database…");
  63.         throw new Error("Error copying database");
  64.       }
  65.     }
  66.  
  67.   }
  68.  
  69.   /**
  70.    * Get the list of splitted files to import in the new Database
  71.    *
  72.    * @return list of files under splitDb/
  73.    */
  74.   private String[] getAssetDbFiles()
  75.   {
  76.     AssetManager mAM = mContext.getAssets();
  77.     String splitFileArray[] = null;
  78.  
  79.     try
  80.     {
  81.       Log.i(TAG, "getAssetDbFiles(): Import multiple files…");
  82.       splitFileArray = mAM.list(SPLIT_FILES_PATH);
  83.  
  84.       if (splitFileArray != null)
  85.       {
  86.         Log.i(TAG, "getAssetDbFiles(): Splitted files found!");
  87.       } else
  88.       {
  89.         Log.v(TAG, "getAssetDbFiles(): NO Splitted files found!"
  90.             + SPLIT_FILES_PATH);
  91.       }
  92.     } catch (IOException e)
  93.     {
  94.       throw new Error("Fail to list: " + SPLIT_FILES_PATH);
  95.     }
  96.  
  97.     return splitFileArray;
  98.   }
  99.  
  100.   /**
  101.    * Check if the database already exist to avoid re-copying the file each time
  102.    * you open the application.
  103.    *
  104.    * @return true if it exists, false if it doesn't
  105.    */
  106.   private boolean checkDataBase()
  107.   {
  108.  
  109.     SQLiteDatabase checkDB = null;
  110.  
  111.     Log.i(TAG, "Opening Database…");
  112.     try
  113.     {
  114.       String mPath = DB_PATH + DB_NAME;
  115.       checkDB = SQLiteDatabase.openDatabase(mPath, null,
  116.           SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
  117.       Log.i(TAG, "checkDataBase(): Database is OPEN");
  118.  
  119.     } catch (SQLiteException e)
  120.     {
  121.       // database does't exist yet.
  122.       Log.i(TAG, "checkDataBase(): Database DOESN'T exists");
  123.     } finally
  124.     // ALWAYS executed even if try fail
  125.     {
  126.       if (checkDB != null)
  127.       {
  128.         checkDB.close();
  129.       }
  130.     }
  131.  
  132.     return checkDB != null ? true : false;
  133.   }
  134.  
  135.   /**
  136.    * Copies your database from your local assets-folder to the just created
  137.    * empty database in the system folder, from where it can be accessed and
  138.    * handled. This is done by transfering bytestream.
  139.    * */
  140.   private void copyDataBase() throws IOException
  141.   {
  142.     String outFileName = DB_PATH + DB_NAME; // Path to the just created empty db
  143.     OutputStream mOutput = null; // Output stream for the Db
  144.     String dbSplitPartName = null; // chunk filename
  145.     InputStream mInput = null; // stream to a chunk
  146.  
  147.     try
  148.     {
  149.       // Open the empty db as the output stream
  150.       mOutput = new FileOutputStream(outFileName);
  151.  
  152.       // Get list of chunk files to copy into the Database
  153.       List<String> splitFileList = new ArrayList<String>();
  154.       String[] chunkArray = getAssetDbFiles();
  155.  
  156.       for (String chunk : chunkArray)
  157.       {
  158.         Log.v(TAG, "copyDataBase(): Importing chunk: " + chunk);
  159.  
  160.         try
  161.         {
  162.           // Open your local db as the input stream
  163.           mInput = mContext.getAssets().open(chunk);
  164.  
  165.           // transfer bytes from the inputfile to the outputfile
  166.           byte[] buffer = new byte[1024];
  167.           int length;
  168.           while ((length = mInput.read(buffer)) > 0)
  169.           {
  170.             mOutput.write(buffer, 0, length);
  171.           }
  172.  
  173.         } catch (IOException e)
  174.         {
  175.           Log.e(TAG, "copyDataBase(): CAN'T OPEN chunk: " + chunk);
  176.           e.printStackTrace();
  177.         }
  178.       }
  179.  
  180.       // // add all element from the Array to the List
  181.       // Collections.addAll(splitFileList, chunkArray);
  182.       //
  183.       // for (Iterator<String> it = splitFileList.iterator(); it.hasNext();)
  184.       // {
  185.       // dbSplitPartName = it.next(); // get next item (starting with none)
  186.       // Log.v(TAG,
  187.       // "copyDataBase(): Importing chunk: " + dbSplitPartName.toString());
  188.       //
  189.       // // Open your local db as the input stream
  190.       // mInput = mContext.getAssets().open(dbSplitPartName);
  191.       //
  192.       // // transfer bytes from the inputfile to the outputfile
  193.       // byte[] buffer = new byte[1024];
  194.       // int length;
  195.       // while ((length = mInput.read(buffer)) > 0)
  196.       // {
  197.       // mOutput.write(buffer, 0, length);
  198.       // }
  199.       // }
  200.     } catch (FileNotFoundException e)
  201.     {
  202.       Log.e(TAG, "copyDataBase(): CAN'T OPEN output stream/file: "
  203.           + outFileName.toString());
  204.       e.printStackTrace();
  205.     } finally
  206.     {
  207.       if (mOutput != null)
  208.       {// Close the streams
  209.         mOutput.flush();
  210.         mOutput.close();
  211.       }
  212.       if (mInput != null)
  213.       {
  214.         mInput.close();
  215.       }
  216.     }
  217.   }
  218.  
  219.   public void openDataBase() throws SQLException
  220.   {
  221.     // Open the database
  222.     String mPath = DB_PATH + DB_NAME;
  223.     db = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.OPEN_READONLY);
  224.   }
  225.  
  226.   @Override
  227.   public synchronized void close()
  228.   {
  229.     if (db != null) db.close();
  230.  
  231.     super.close();
  232.   }
  233.  
  234.   @Override
  235.   public void onCreate(SQLiteDatabase db)
  236.   {
  237.   }
  238.  
  239.   @Override
  240.   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  241.   {
  242.   }
  243.  
  244.   // Add your public helper methods to access and get content from the database.
  245.   // You could return cursors by doing "return mDataBase.query(....)" so it'd
  246.   // be easy
  247.   // to you to create adapters for your views.
  248.  
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement