Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- 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;
- public class DBHandler extends SQLiteOpenHelper{
- String DB_PATH = null;
- private static String DB_NAME = "rehoboth.db";
- private SQLiteDatabase myDatabase;
- private final Context myContext;
- public DBHandler(Context context){
- super(context, DB_NAME, null, 2);
- this.myContext = context;
- this.DB_PATH = "/data/data" + context.getPackageName() + "/databases/";
- Log.e("Path 1", DB_PATH);
- }
- public void createDatabase() throws IOException {
- boolean dbExist = checkDatabase();
- if(dbExist){
- /* openDatabase();
- int cVersion = myDatabase.getVersion();
- if(cVersion != 2){
- onUpgrade(myDatabase, myDatabase.getVersion(),2);
- close();
- }*/
- }else{
- this.getReadableDatabase();
- try{
- copyDatabase();
- }catch (IOException e){
- throw new Error("Error copying database");
- }
- }
- }
- private boolean checkDatabase(){
- SQLiteDatabase checkDB = null;
- try{
- String myPath = DB_PATH + DB_NAME;
- checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
- }catch(SQLiteException e){
- }
- if(checkDB != null){
- checkDB.close();
- }
- return checkDB != null ? true : false;
- }
- private void copyDatabase() throws IOException{
- InputStream myInput = myContext.getAssets().open(DB_NAME);
- String outFileName = DB_PATH + DB_NAME;
- OutputStream myOutput = new FileOutputStream(outFileName);
- byte[] buffer = new byte[10];
- int length;
- while((length = myInput.read(buffer)) > 0) {
- myOutput.write(buffer, 0, length);
- }
- myOutput.flush();
- myOutput.close();
- myInput.close();
- }
- public void openDatabase() throws SQLException{
- String myPath = DB_PATH + DB_NAME;
- 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){
- if(newVersion > oldVersion)
- try{
- copyDatabase();
- }catch (IOException e){
- e.printStackTrace();
- }
- }
- public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy){
- return myDatabase.query("rehoboth", null, null, null, null, null, null);
- }
- }
- import android.app.Activity;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- import java.io.IOException;
- public class DBcopyActivity extends Activity {
- Cursor c = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_dbcopy);
- ((Button) findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v){
- DBHandler myDbHelper = new DBHandler(DBcopyActivity.this);
- try{
- myDbHelper.createDatabase();
- }catch(IOException ioe){
- throw new Error("Unable to create database");
- }try{
- myDbHelper.openDatabase();
- }catch(SQLException sqle){
- throw sqle;
- }
- Toast.makeText(DBcopyActivity.this, "Successfully Imported", Toast.LENGTH_SHORT).show();
- c = myDbHelper.query("rehoboth", null, null, null, null, null, null);
- if(c.moveToFirst()){
- do{
- Toast.makeText(DBcopyActivity.this,
- "_id: " + c.getString(0) + "n" +
- "NAME: " + c.getString(1) + "n" +
- "PASSWORD: " + c.getString(2),
- Toast.LENGTH_LONG).show();
- }while (c.moveToNext());
- }
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment