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/"
                 OR
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"(/data/user/0/com.abc.appName/files/LoginDb.db), 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]
copyAppDbToDownloadFolder("LoginDb.db","LoginDb_dest.db");
   
   /// <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);
  //dest
  var destFile = new Java.IO.File(destPath);
  if (!destFile.Exists())
  {
   destFile.CreateNewFile();
  }   
 //source
 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());
   src.Close();
   dst.Close();
  }
 }
Now you can transfer your DB file from device download folder to machine and open using sqlite db browser(http://sqlitebrowser.org/)

2 comments:

  1. public class LoginDbHelper : SQLiteOpenHelper
    {
    private const string APP_DATABASENAME = "App.db";
    private const int APP_DATABASE_VERSION = 1;


    public LoginDbHelper(Context ctx) : base(ctx, APP_DATABASENAME, null, APP_DATABASE_VERSION)
    {

    }
    public override void OnCreate(SQLiteDatabase db)
    {
    db.ExecSQL(@"CREATE TABLE IF NOT EXISTS Login(
    Id INTEGER PRIMARY KEY AUTOINCREMENT,
    UserName TEXT NOT NULL,
    Password TEXT NOT NULL)");
    }
    public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
    OnCreate(db);
    }
    this is my code how to Reterive my App.db to App_backup.db

    ReplyDelete
    Replies
    1. Check in which path your db file saved. if it is same same as above mentioned location(System.Environment.SpecialFolder.Personal), then just run the above method from your activity class. If it is in diffrent location just change the source path inside the method accordingly.

      Delete