July 16, 2017

How to fetch android sqlite db file from device in Mac Xamarin/Visul Studio.

Here i'm going to share one of the simple method to fetch sqlite database file from android device for xamarin or visul studio installed in mac machine. 
Recently when i was checking data stored in the sqlite db from android device, i couldn't do it easily as i fail to run ADB commands from my terminal.

Same if you are fetching DB file from Emulator it is pretty straight forward. 
Launch Device Monitor(Tools-> Device Monitor) 
select Emulator from left pane
select File Explorer from right pane 
Navigate to app data base file-> /data/data/com.appName.MyApp/databases/
Select required database file->"Pull a file from device" option from top right as shown in screen
Copy DB file to machine storage and open using sqlite db browser(http://sqlitebrowser.org/)

To fetch DB from android device using Windows machine we can use the following ADB command
Open cmd
Run these commands,

[Navigate to ADB path]
C:\Program Files (x86)\Android\android-sdk\platform-tools 
[List database files,enter your app package name by replacing com.myCom.myApp]
adb -d shell "run-as com.myCom.myApp ls /data/data/com.myCom.myApp/database/"
adb -d shell "run-as com.myCom.myApp ls /data/data/com.myCom.myApp/files/"
[Concatinate db file to sd card]
adb -d shell "run-as com.myCom.myApp cat /data/data/com.myCom.myApp/files/localstore.db> /sdcard/localstore.db"  
[Receieve sqlite file in sd card]
adb pull /sdcard/localstore.db   
Copy sqlite file from your sd card to your system.

To fetch DB file using MAC machine same ADB command can also be used.
As an alternative you can also do it by programmatically to copy DB file to device download folder. Run this code snippet from Main.activity file.
Here for sourcePath in my case it is in "System.Environment.SpecialFolder.Personal", ensure this path is same as the path you used to save the DB file. Provide WRITE_EXTERNAL_STORAGE permission[Note:if you are not implemented runtime permission target it to api level >22]
   /// <summary>
   /// Copies the app db to device  download folder.
   /// </summary>
   /// <param name="sourceDbFileName" />Source db file name.
   /// <param name="destDbFileName" />Destination db file name.
 void copyAppDbToDownloadFolder(string sourceDbFileName,string destDbFileName )
      var destPath = System.IO.Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath, destDbFileName);
  var destFile = new Java.IO.File(destPath);
  if (!destFile.Exists())
 var sourcePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
 var srcPath = System.IO.Path.Combine(sourcePath, sourceDbFileName); 
 var currentDB = GLOBAL_CONTEXT.GetDatabasePath(srcPath); 
 if (currentDB.Exists())
   Java.Nio.Channels.FileChannel src = new Java.IO.FileInputStream(currentDB).Channel;  
   Java.Nio.Channels.FileChannel dst = new Java.IO.FileOutputStream(destFile).Channel;
   dst.TransferFrom(src, 0, src.Size());
Now you can transfer your DB file from device download folder to machine and open using sqlite db browser(http://sqlitebrowser.org/)