Advertisement
Guest User

Untitled

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