Guest User

Untitled

a guest
Feb 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. import android.content.Context;
  2. import android.database.Cursor;
  3. import android.database.SQLException;
  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.  
  14. public class DBHandler extends SQLiteOpenHelper{
  15.  
  16. String DB_PATH = null;
  17. private static String DB_NAME = "rehoboth.db";
  18. private SQLiteDatabase myDatabase;
  19. private final Context myContext;
  20.  
  21. public DBHandler(Context context){
  22. super(context, DB_NAME, null, 2);
  23. this.myContext = context;
  24. this.DB_PATH = "/data/data" + context.getPackageName() + "/databases/";
  25. Log.e("Path 1", DB_PATH);
  26. }
  27.  
  28. public void createDatabase() throws IOException {
  29. boolean dbExist = checkDatabase();
  30. if(dbExist){
  31. /* openDatabase();
  32. int cVersion = myDatabase.getVersion();
  33. if(cVersion != 2){
  34. onUpgrade(myDatabase, myDatabase.getVersion(),2);
  35. close();
  36. }*/
  37. }else{
  38. this.getReadableDatabase();
  39. try{
  40. copyDatabase();
  41. }catch (IOException e){
  42. throw new Error("Error copying database");
  43. }
  44. }
  45. }
  46.  
  47. private boolean checkDatabase(){
  48. SQLiteDatabase checkDB = null;
  49. try{
  50. String myPath = DB_PATH + DB_NAME;
  51. checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  52. }catch(SQLiteException e){
  53. }
  54. if(checkDB != null){
  55. checkDB.close();
  56. }
  57. return checkDB != null ? true : false;
  58. }
  59.  
  60.  
  61. private void copyDatabase() throws IOException{
  62. InputStream myInput = myContext.getAssets().open(DB_NAME);
  63. String outFileName = DB_PATH + DB_NAME;
  64. OutputStream myOutput = new FileOutputStream(outFileName);
  65. byte[] buffer = new byte[10];
  66. int length;
  67. while((length = myInput.read(buffer)) > 0) {
  68. myOutput.write(buffer, 0, length);
  69. }
  70. myOutput.flush();
  71. myOutput.close();
  72. myInput.close();
  73. }
  74.  
  75. public void openDatabase() throws SQLException{
  76. String myPath = DB_PATH + DB_NAME;
  77. myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  78. }
  79.  
  80. @Override
  81. public synchronized void close(){
  82. if(myDatabase != null)
  83. myDatabase.close();
  84. super.close();
  85. }
  86.  
  87. @Override
  88. public void onCreate(SQLiteDatabase db){
  89.  
  90. }
  91.  
  92. @Override
  93. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
  94. if(newVersion > oldVersion)
  95. try{
  96. copyDatabase();
  97. }catch (IOException e){
  98. e.printStackTrace();
  99. }
  100. }
  101.  
  102. public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy){
  103. return myDatabase.query("rehoboth", null, null, null, null, null, null);
  104. }
  105. }
  106.  
  107. import android.app.Activity;
  108. import android.database.Cursor;
  109. import android.database.SQLException;
  110. import android.support.v7.app.AppCompatActivity;
  111. import android.os.Bundle;
  112. import android.view.View;
  113. import android.widget.Button;
  114. import android.widget.Toast;
  115.  
  116. import java.io.IOException;
  117.  
  118. public class DBcopyActivity extends Activity {
  119.  
  120. Cursor c = null;
  121.  
  122. @Override
  123. protected void onCreate(Bundle savedInstanceState) {
  124. super.onCreate(savedInstanceState);
  125. setContentView(R.layout.activity_dbcopy);
  126.  
  127. ((Button) findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener(){
  128. @Override
  129. public void onClick(View v){
  130. DBHandler myDbHelper = new DBHandler(DBcopyActivity.this);
  131. try{
  132. myDbHelper.createDatabase();
  133. }catch(IOException ioe){
  134. throw new Error("Unable to create database");
  135. }try{
  136. myDbHelper.openDatabase();
  137. }catch(SQLException sqle){
  138. throw sqle;
  139. }
  140. Toast.makeText(DBcopyActivity.this, "Successfully Imported", Toast.LENGTH_SHORT).show();
  141. c = myDbHelper.query("rehoboth", null, null, null, null, null, null);
  142. if(c.moveToFirst()){
  143. do{
  144. Toast.makeText(DBcopyActivity.this,
  145. "_id: " + c.getString(0) + "n" +
  146. "NAME: " + c.getString(1) + "n" +
  147. "PASSWORD: " + c.getString(2),
  148. Toast.LENGTH_LONG).show();
  149. }while (c.moveToNext());
  150. }
  151.  
  152. }
  153. });
  154. }
  155. }
Add Comment
Please, Sign In to add comment