Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //The Android's default system path of your application database.
- private static String DB_PATH = "/data/data/enkend.apps.proyects.optc.testapp/databases/";
- private static String DB_NAME = "Test.db";
- private SQLiteDatabase myDataBase;
- private final Context myContext;
- //private String DB_PATH;
- /**
- * Constructor
- * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
- * @param context
- */
- public DataBaseHelper(Context context) {
- super(context, DB_NAME, null, 1);
- this.myContext = context;
- }
- /**
- * Creates a empty database on the system and rewrites it with your own database.
- * */
- public void createDataBase() throws IOException {
- myContext.deleteDatabase(DB_NAME);
- boolean dbExist = checkDataBase();
- if(dbExist){
- //do nothing - database already exist
- copyDataBase();
- }else{
- //By calling this method and empty database will be created into the default system path
- //of your application so we are gonna be able to overwrite that database with our database.
- this.getReadableDatabase();
- try {
- copyDataBase();
- } catch (IOException e) {
- throw new Error("Error copying database");
- }
- }
- }
- /**
- * Check if the database already exist to avoid re-copying the file each time you open the application.
- * @return true if it exists, false if it doesn't
- */
- private boolean checkDataBase(){
- File databasePath = myContext.getDatabasePath(DB_NAME);
- return databasePath.exists();
- }
- /**
- * Copies your database from your local assets-folder to the just created empty database in the
- * system folder, from where it can be accessed and handled.
- * This is done by transfering bytestream.
- * */
- private void copyDataBase() throws IOException{
- //Open your local db as the input stream
- InputStream myInput = myContext.getAssets().open(DB_NAME);
- // Path to the just created empty db
- String outFileName = DB_PATH;
- //Open the empty db as the output stream
- OutputStream myOutput = new FileOutputStream(outFileName);
- //transfer bytes from the inputfile to the outputfile
- byte[] buffer = new byte[1024];
- int length;
- while ((length = myInput.read(buffer))>0){
- myOutput.write(buffer, 0, length);
- }
- //Close the streams
- myOutput.flush();
- myOutput.close();
- myInput.close();
- }
- public void openDataBase() throws SQLException {
- //Open the database
- String myPath = DB_PATH;
- myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
- }
- @Override
- public synchronized void close() {
- if(myDataBase != null)
- myDataBase.close();
- super.close();
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- public EventType getEventType(String idEventType) {
- EventType fn = new EventType();
- SQLiteDatabase db = this.getReadableDatabase();
- //specify the columns to be fetched
- String[] columns = {"_id", "Name", "ImgURL", "Notes","Permanent"};
- //Select condition
- String selection = "_id = ?";
- //Arguments for selection
- String[] selectionArgs = {idEventType};
- Cursor cursor = db.query("Fortnight", columns, selection,
- selectionArgs, null, null, null);
- if (null != cursor) {
- cursor.moveToFirst();
- fn.idEventType(cursor.getInt(0));
- fn.setName(cursor.getString(1));
- fn.setImgURL(cursor.getString(2));
- fn.setNotes(cursor.getString(3));
- fn.setPermanent(cursor.getInt(4));
- }
- db.close();
- return fn;
- }
- private void databaseConexion()
- {
- dbHelper = new DataBaseHelper(this);
- try {
- dbHelper.createDataBase();
- } catch (IOException ioe) {
- throw new Error("Unable to create database");
- }
- try {
- dbHelper.openDataBase();
- }catch(SQLException sqle){
- throw sqle;
- }
- }
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement