Advertisement
Guest User

DataBaseHelper.java

a guest
Jun 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. package com.example.johhnyfrog.myapplication;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8.  
  9. import android.content.Context;
  10. import android.database.Cursor;
  11. import android.database.SQLException;
  12. import android.database.sqlite.SQLiteDatabase;
  13. import android.database.sqlite.SQLiteOpenHelper;
  14. import android.util.Log;
  15.  
  16. public class DataBaseHelper extends SQLiteOpenHelper
  17. {
  18. private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
  19. //destination path (location) of our database on device
  20. private static String DB_PATH = "";
  21. private static String DB_NAME ="domande";// Database name
  22. private SQLiteDatabase mDataBase;
  23. private final Context mContext;
  24.  
  25. public DataBaseHelper(Context context)
  26. {
  27. super(context, DB_NAME, null, 1);// 1? Its database Version
  28. if(android.os.Build.VERSION.SDK_INT >= 17){
  29. System.out.println(context.getApplicationInfo().dataDir);
  30. DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
  31. }
  32. else
  33. {
  34. DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
  35. }
  36. this.mContext = context;
  37. }
  38.  
  39. @Override
  40. public void onCreate(SQLiteDatabase mDataBase){} //aggiunto perchè altrimente chiede che sia una classe astratta
  41. public void onUpgrade(SQLiteDatabase db, int p,int d){db.execSQL("DROP TABLE IF EXISTS libri");
  42. onCreate(db);} //aggiunto perchè altrimente chiede che sia una classe astratta
  43.  
  44.  
  45. public void createDataBase() throws IOException
  46. {
  47. //If the database does not exist, copy it from the assets.
  48.  
  49. boolean mDataBaseExist = checkDataBase();
  50. if(!mDataBaseExist)
  51. {
  52. this.getWritableDatabase();
  53. this.close();
  54. try
  55. {
  56. //Copy the database from assests
  57. copyDataBase();
  58. Log.e(TAG, "createDatabase database created");
  59. }
  60. catch (IOException mIOException)
  61. {
  62. throw new Error("ErrorCopyingDataBase");
  63. }
  64. }
  65. }
  66.  
  67. //Check that the database exists here: /data/data/your package/databases/Da Name
  68. private boolean checkDataBase()
  69. {
  70. File dbFile = new File(DB_PATH + DB_NAME);
  71. //Log.v("dbFile", dbFile + " "+ dbFile.exists());
  72. return dbFile.exists();
  73. }
  74.  
  75. //Copy the database from assets
  76. private void copyDataBase() throws IOException
  77. {
  78. InputStream mInput = mContext.getAssets().open(DB_NAME);
  79. String outFileName = DB_PATH + DB_NAME;
  80. OutputStream mOutput = new FileOutputStream(outFileName);
  81. byte[] mBuffer = new byte[1024];
  82. int mLength;
  83. while ((mLength = mInput.read(mBuffer))>0)
  84. {
  85. mOutput.write(mBuffer, 0, mLength);
  86. }
  87. mOutput.flush();
  88. mOutput.close();
  89. mInput.close();
  90. }
  91.  
  92. //Open the database, so we can query it
  93. public boolean openDataBase() throws SQLException
  94. {
  95. String mPath = DB_PATH + DB_NAME;
  96. //Log.v("mPath", mPath);
  97. mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
  98. //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
  99. return mDataBase != null;
  100. }
  101.  
  102. @Override
  103. public synchronized void close()
  104. {
  105. if(mDataBase != null)
  106. mDataBase.close();
  107. super.close();
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement