Advertisement
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 DataBaseHelper extends SQLiteOpenHelper {
- String DB_PATH = null;
- private static String DB_NAME = "BeerDB";
- private SQLiteDatabase myDataBase;
- private final Context myContext;
- public DataBaseHelper(Context context) {
- super(context, DB_NAME, null, 10);
- 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) {
- } 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("PLEASE CHANGE TO YOUR TABLE NAME", null, null, null, null, null, null);
- }
- }
- import android.database.Cursor;
- import android.database.SQLException;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- public class CopyDbActivity extends AppCompatActivity {
- Cursor c = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_copy_db);
- ((Button) findViewById(R.id.button01)).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- DatabaseHelper myDbHelper = new DatabaseHelper(CopyDbActivity.this);
- try {
- myDbHelper.createDataBase();
- } catch (IOException ioe) {
- throw new Error("Unable to create database");
- }
- try {
- myDbHelper.openDataBase();
- } catch (SQLException sqle) {
- throw sqle;
- }
- Toast.makeText(CopyDbActivity.this, "Successfully Imported", Toast.LENGTH_SHORT).show();
- c = myDbHelper.query("Products", null, null, null, null, null, null);
- if (c.moveToFirst()) {
- do {
- Toast.makeText(CopyDbActivity.this,
- "_id: " + c.getString(0) + "n" +
- "DATE: " + c.getString(1) + "n" +
- "TIME: " + c.getString(2) + "n" +
- "HEIGHT: " + c.getString(3),
- Toast.LENGTH_LONG).show();
- } while (c.moveToNext());
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement