Sep 2, 2011

Android - Copy sqlite database from device to PC

Android supports SQLite database. It is placed in /data/data/<package name>/databases folder. When we are working with Emulator. we can access /data/data/<package name>/database folder. We can pull database from Emulator by using DDMS or adb pull comment. As we know that mobile device does not allow data/data folder for security reason. We can not access /data/data folder by using DDMS or abd pull. It may create problem for debugging. To solve this problem following function can use. Following function copy sqlite database from device to PC.

public void copyDatabaseToSdCard() {
		Log.e("Databasehealper", "********************************");
		try {
			File f1 = new File("DATABASE PATH AND NAME");
			if (f1.exists()) {

				File f2 = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+ "/My_App_Database.db");
				InputStream in = new FileInputStream(f1);
				OutputStream out = new FileOutputStream(f2);
				byte[] buf = new byte[1024];
				int len;
				while ((len = > 0) {
					out.write(buf, 0, len);
		} catch (FileNotFoundException ex) {
			System.out.println(ex.getMessage() + " in the specified directory.");
		} catch (IOException e) {
		Log.e("Databasehealper", "********************************");

Generally I use this function in "" class. In Above function you have to replace "DATABASE PATH AND NAME" (i.e. /data/data/com.kpbird.sqldemo/databases/mydatabase.db) value. the function copy your database in root of sdcard with "My_App_Database.db" name.