-1

I been working on my project for a little bit while, I currently using emulator to access the DB I created. However, I wonder if anyone knows a way to access DB on a real device in DDMS eclipse?

I understand that to access DB on emulator is just open the data/data/package_name/database...I couldn't really find a way to check out DB on my real device. (The is some security issue in android device) The reason I want to use this is sometimes, emulator doesn't support GPS signal. Does any one knows is there any third-party lib/plugin I can download from ? Thank you so much.

bj4947
  • 880
  • 11
  • 32
  • There's a method to export a sqlite database and save it as a file in your directory in a real device. – Razgriz Aug 26 '14 at 03:21
  • You really need to copy the DB to somewhere public (prolly SD card). [This question](http://stackoverflow.com/questions/1995320/how-to-backup-database-file-to-sdcard-on-android) handles that case. – Andrew T. Aug 26 '14 at 03:22
  • you can access your application database only(if unrooted). – Viswanath Lekshmanan Aug 26 '14 at 05:11

1 Answers1

1

i think it is work for you **I am copy database in SdCard then access ** .

it is my database helper class

public class OpenDatabaseHelper extends SQLiteOpenHelper {

static final String DATABASE_NAME = "MyDB";
.
.
.
public OpenDatabaseHelper(Context context) {
        // super(context, name, factory, version);
        super(context, Environment.getExternalStorageDirectory()
                + File.separator + "/DataBase/" + File.separator
                + DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }
.
.
.
}

create object in Activity class

OpenDatabaseHelper db = new OpenDatabaseHelper(YourActivity.this);

call any method of database

db.DataBasemethod();

then put this code

try {
            String destPath = Environment.getExternalStorageDirectory().toString();
            File f = new File(destPath);
            if (!f.exists()) {
                f.mkdirs();
                f.createNewFile();
                // ---copy the db from the /data/data/ folder into
                // the sdcard databases folder--- here MyDB is database name
                CopyDB(new FileInputStream("/data/data/" + getPackageName()+ "/databases"), new FileOutputStream(destPath+ "/MyDB"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }

public void CopyDB(InputStream inputStream, OutputStream outputStream)
            throws IOException {
        // ---copy 1K bytes at a time---
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
        inputStream.close();
        outputStream.close();
    }
Mr X
  • 1,053
  • 2
  • 11
  • 24