hm-lok

Unnamed repository; edit this file 'description' to name the repository.
git clone http://git.code.weiherhei.de/hm-lok.git
Log | Files | Refs

DataBaseHelper.java (3983B)


      1 package de.jandankert.hansemerkur.db;
      2 
      3 import java.io.FileOutputStream;
      4 import java.io.IOException;
      5 import java.io.InputStream;
      6 import java.io.OutputStream;
      7 
      8 import android.content.Context;
      9 import android.database.SQLException;
     10 import android.database.sqlite.SQLiteDatabase;
     11 import android.database.sqlite.SQLiteException;
     12 import android.database.sqlite.SQLiteOpenHelper;
     13 import android.util.Log;
     14 
     15 public class DataBaseHelper extends SQLiteOpenHelper
     16 {
     17 
     18 	private static final int VERSION = 2;
     19 
     20 	// The Android's default system path of your application database.
     21 	private static String DB_PATH = "/data/data/de.jandankert.hansemerkur/databases/";
     22 
     23 	private static String DB_NAME = "hm.db";
     24 
     25 	private SQLiteDatabase myDataBase;
     26 
     27 	private final Context myContext;
     28 
     29 	/**
     30 	 * Constructor Takes and keeps a reference of the passed context in order to
     31 	 * access to the application assets and resources.
     32 	 * 
     33 	 * @param context
     34 	 */
     35 	public DataBaseHelper(Context context)
     36 	{
     37 
     38 		super(context, DB_NAME, null, VERSION);
     39 		this.myContext = context;
     40 	}
     41 
     42 	/**
     43 	 * Creates a empty database on the system and rewrites it with your own
     44 	 * database.
     45 	 * */
     46 	public void createDataBase() throws IOException
     47 	{
     48 
     49 		boolean dbExist = checkDataBase();
     50 		// dbExist = false;
     51 
     52 		if (dbExist)
     53 		{
     54 			// do nothing - database already exist
     55 		} else
     56 		{
     57 
     58 			// By calling this method and empty database will be created into
     59 			// the default system path
     60 			// of your application so we are gonna be able to overwrite that
     61 			// database with our database.
     62 			this.getReadableDatabase();
     63 
     64 			try
     65 			{
     66 
     67 				copyDataBase();
     68 
     69 			} catch (IOException e)
     70 			{
     71 
     72 				throw new Error("Error copying database");
     73 
     74 			}
     75 		}
     76 
     77 	}
     78 
     79 	/**
     80 	 * Check if the database already exist to avoid re-copying the file each
     81 	 * time you open the application.
     82 	 * 
     83 	 * @return true if it exists, false if it doesn't
     84 	 */
     85 	private boolean checkDataBase()
     86 	{
     87 
     88 		SQLiteDatabase checkDB = null;
     89 
     90 		try
     91 		{
     92 			String myPath = DB_PATH + DB_NAME;
     93 			checkDB = SQLiteDatabase.openDatabase(myPath, null,
     94 					SQLiteDatabase.OPEN_READONLY
     95 							| SQLiteDatabase.NO_LOCALIZED_COLLATORS);
     96 
     97 		} catch (SQLiteException e)
     98 		{
     99 
    100 			// database does't exist yet.
    101 
    102 		}
    103 
    104 		if (checkDB != null)
    105 		{
    106 
    107 			checkDB.close();
    108 
    109 		}
    110 
    111 		return checkDB != null;
    112 	}
    113 
    114 	/**
    115 	 * Copies your database from your local assets-folder to the just created
    116 	 * empty database in the system folder, from where it can be accessed and
    117 	 * handled. This is done by transfering bytestream.
    118 	 * */
    119 	private void copyDataBase() throws IOException
    120 	{
    121 
    122 		// Open your local db as the input stream
    123 		InputStream myInput = myContext.getAssets().open(DB_NAME);
    124 
    125 		// Path to the just created empty db
    126 		String outFileName = DB_PATH + DB_NAME;
    127 
    128 		// Open the empty db as the output stream
    129 		OutputStream myOutput = new FileOutputStream(outFileName);
    130 
    131 		// transfer bytes from the inputfile to the outputfile
    132 		byte[] buffer = new byte[1024];
    133 		int length;
    134 		while ((length = myInput.read(buffer)) > 0)
    135 		{
    136 			myOutput.write(buffer, 0, length);
    137 		}
    138 
    139 		// Close the streams
    140 		myOutput.flush();
    141 		myOutput.close();
    142 		myInput.close();
    143 
    144 	}
    145 
    146 	public void openDataBase() throws SQLException
    147 	{
    148 
    149 		// Open the database
    150 		String myPath = DB_PATH + DB_NAME;
    151 		myDataBase = SQLiteDatabase.openDatabase(myPath, null,
    152 				SQLiteDatabase.OPEN_READONLY
    153 						| SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    154 
    155 	}
    156 
    157 	@Override
    158 	public synchronized void close()
    159 	{
    160 
    161 		if (myDataBase != null)
    162 			myDataBase.close();
    163 
    164 		super.close();
    165 
    166 	}
    167 
    168 	@Override
    169 	public void onCreate(SQLiteDatabase db)
    170 	{
    171 
    172 	}
    173 
    174 	@Override
    175 	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    176 	{
    177 		
    178 //		try
    179 //		{
    180 //			this.copyDataBase();
    181 //		} catch (IOException e)
    182 //		{
    183 //			Log.w("DB",e);
    184 //		}
    185 	}
    186 
    187 	// Add your public helper methods to access and get content from the
    188 	// database.
    189 	// You could return cursors by doing "return myDataBase.query(....)" so it'd
    190 	// be easy
    191 	// to you to create adapters for your views.
    192 
    193 }