Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // package goes here
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteException;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Locale;
- public class DatabaseHelper extends SQLiteOpenHelper {
- private static final String DB_PATH = "PATH";
- private static final String DB_NAME = "DB";
- private final Context myContext;
- public SQLiteDatabase db;
- public DatabaseHelper(Context context) {
- super(context, DB_NAME, null, 1);
- this.myContext = context;
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- public void createDatabase() {
- createDB();
- }
- private void createDB() {
- boolean dbExist = DBExists();
- if(!dbExist) {
- this.getReadableDatabase();
- copyDBFromResource();
- }
- }
- private boolean DBExists() {
- SQLiteDatabase db = null;
- try {
- String databasePath = DB_PATH + DB_NAME;
- db = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
- db.setLocale(Locale.getDefault());
- db.setLockingEnabled(true);
- db.setVersion(1);
- } catch (SQLiteException e) {
- Log.e("SqlHelper", "database not found");
- }
- if (db != null) {
- db.close();
- }
- return db != null ? true : false;
- }
- private void copyDBFromResource() {
- InputStream inputStream = null;
- OutputStream outStream = null;
- String dbFilePath = DB_PATH + DB_NAME;
- try {
- inputStream = myContext.getAssets().open(DB_NAME);
- outStream = new FileOutputStream(dbFilePath);
- byte[] buffer = new byte[1024];
- int length;
- while ((length = inputStream.read(buffer)) > 0) {
- outStream.write(buffer, 0, length);
- }
- outStream.flush();
- outStream.close();
- inputStream.close();
- } catch (IOException e) {
- throw new Error("Problem copying database from resource file.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement