Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. //The Android's default system path of your application database.
  2. private static String DB_PATH = "/data/data/enkend.apps.proyects.optc.testapp/databases/";
  3.  
  4.  
  5. private static String DB_NAME = "Test.db";
  6.  
  7. private SQLiteDatabase myDataBase;
  8.  
  9. private final Context myContext;
  10.  
  11. //private String DB_PATH;
  12.  
  13. /**
  14. * Constructor
  15. * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
  16. * @param context
  17. */
  18. public DataBaseHelper(Context context) {
  19.  
  20. super(context, DB_NAME, null, 1);
  21. this.myContext = context;
  22. }
  23.  
  24. /**
  25. * Creates a empty database on the system and rewrites it with your own database.
  26. * */
  27. public void createDataBase() throws IOException {
  28.  
  29. myContext.deleteDatabase(DB_NAME);
  30. boolean dbExist = checkDataBase();
  31.  
  32. if(dbExist){
  33. //do nothing - database already exist
  34. copyDataBase();
  35. }else{
  36.  
  37. //By calling this method and empty database will be created into the default system path
  38. //of your application so we are gonna be able to overwrite that database with our database.
  39. this.getReadableDatabase();
  40.  
  41. try {
  42.  
  43. copyDataBase();
  44.  
  45. } catch (IOException e) {
  46.  
  47. throw new Error("Error copying database");
  48.  
  49. }
  50. }
  51.  
  52. }
  53.  
  54. /**
  55. * Check if the database already exist to avoid re-copying the file each time you open the application.
  56. * @return true if it exists, false if it doesn't
  57. */
  58.  
  59. private boolean checkDataBase(){
  60.  
  61. File databasePath = myContext.getDatabasePath(DB_NAME);
  62. return databasePath.exists();
  63. }
  64.  
  65. /**
  66. * Copies your database from your local assets-folder to the just created empty database in the
  67. * system folder, from where it can be accessed and handled.
  68. * This is done by transfering bytestream.
  69. * */
  70. private void copyDataBase() throws IOException{
  71.  
  72. //Open your local db as the input stream
  73. InputStream myInput = myContext.getAssets().open(DB_NAME);
  74.  
  75. // Path to the just created empty db
  76. String outFileName = DB_PATH;
  77.  
  78. //Open the empty db as the output stream
  79. OutputStream myOutput = new FileOutputStream(outFileName);
  80.  
  81. //transfer bytes from the inputfile to the outputfile
  82. byte[] buffer = new byte[1024];
  83. int length;
  84. while ((length = myInput.read(buffer))>0){
  85. myOutput.write(buffer, 0, length);
  86. }
  87.  
  88. //Close the streams
  89. myOutput.flush();
  90. myOutput.close();
  91. myInput.close();
  92.  
  93. }
  94.  
  95. public void openDataBase() throws SQLException {
  96.  
  97. //Open the database
  98. String myPath = DB_PATH;
  99. myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  100.  
  101. }
  102.  
  103. @Override
  104. public synchronized void close() {
  105.  
  106. if(myDataBase != null)
  107. myDataBase.close();
  108.  
  109. super.close();
  110.  
  111. }
  112.  
  113. @Override
  114. public void onCreate(SQLiteDatabase db) {
  115.  
  116. }
  117.  
  118. @Override
  119. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  120.  
  121. }
  122.  
  123.  
  124. public EventType getEventType(String idEventType) {
  125.  
  126. EventType fn = new EventType();
  127. SQLiteDatabase db = this.getReadableDatabase();
  128. //specify the columns to be fetched
  129. String[] columns = {"_id", "Name", "ImgURL", "Notes","Permanent"};
  130. //Select condition
  131. String selection = "_id = ?";
  132. //Arguments for selection
  133. String[] selectionArgs = {idEventType};
  134.  
  135. Cursor cursor = db.query("Fortnight", columns, selection,
  136. selectionArgs, null, null, null);
  137. if (null != cursor) {
  138. cursor.moveToFirst();
  139. fn.idEventType(cursor.getInt(0));
  140. fn.setName(cursor.getString(1));
  141. fn.setImgURL(cursor.getString(2));
  142. fn.setNotes(cursor.getString(3));
  143. fn.setPermanent(cursor.getInt(4));
  144. }
  145. db.close();
  146. return fn;
  147. }
  148.  
  149. private void databaseConexion()
  150. {
  151. dbHelper = new DataBaseHelper(this);
  152. try {
  153.  
  154. dbHelper.createDataBase();
  155.  
  156. } catch (IOException ioe) {
  157.  
  158. throw new Error("Unable to create database");
  159. }
  160.  
  161. try {
  162.  
  163. dbHelper.openDataBase();
  164.  
  165. }catch(SQLException sqle){
  166. throw sqle;
  167.  
  168. }
  169. }
  170.  
  171. <uses-permission android:name="android.permission.INTERNET" />
  172. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  173. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement