0

I have a button to backup my local database(SQLITE) to another path so that I can email the file to me. How can I copy the SQLite data to my for example downloads folder?

Here is the code of my backup button:

    private void BtnBackup_Clicked(object sender, EventArgs e)
    {
        string fileName = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "backend.db3");
        string backupfile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "backup.db3");
        File.Copy(fileName, backupfile, true);
    }

I can check if the data has been copied the problem is when I go to the directory no files is being shown

  • 1
    Close all SqliteConnections and copy it to to the `GetExternalFilesDir` location and then you can copy to your PC/Mac via adb, copy/open via Device Monitor, etc... https://stackoverflow.com/questions/54126671/access-the-android-special-folder-path-by-using-environment/54127487#54127487 – SushiHangover Apr 04 '19 at 06:08
  • @SushiHangover Can I just create the backupfile and upload it somewhere? –  Apr 04 '19 at 06:13
  • `upload it somewhere` ? Are you talking about a cloud service? Personal website? Or? – SushiHangover Apr 04 '19 at 06:14
  • @SushiHangover I mean for example I have a backup button in my app then the backup file will be created then the file will be uploaded manually to for example google drive –  Apr 04 '19 at 06:15
  • The location returned from `GetExternalFilesDir` is external to your app's secure sandbox and thus would be available to other applications on that emulator|device. – SushiHangover Apr 04 '19 at 06:17
  • Is there a way to get the sqlite database backup file and upload it? –  Apr 04 '19 at 06:26
  • You said `..uploaded manually..`, so confused by that comment... If you copy the DB file externally to your app's sandbox (i.e. to the location returned via `GetExternalFilesDir`) then other apps can access it (fire up Google Drive, select the file and have Drive upload it to the Google account that is signed up to the device (or call the 3rd-party app via Intent from your app to "automatically" upload it to the user's cloud account: example: https://stackoverflow.com/a/16986759/4984832) – SushiHangover Apr 04 '19 at 06:35
  • @SushiHangover my testers are far away from me that is why I need them to upload the backup file manually –  Apr 04 '19 at 06:57
  • Then you could use a cloud-based blob storage (like Azure or AWS) and programmatically upload it or to your own website via an httplclient's POST, etc... If you want a "manual" system, just have the user's email it to you, upload it to their personal cloud storage account and share a link with you, etc... – SushiHangover Apr 04 '19 at 07:01
  • There is no way to generate a backup file? –  Apr 04 '19 at 07:21
  • Backup file? There is a SQLite online backup API (i.e it can be used while the DB file is in-use: https://sqlite.org/backup.html), otherwise you can just copy the file to make a backup (using the C# `File.Copy` method). – SushiHangover Apr 04 '19 at 07:24
  • Can you show me how to add file copy? using the code in my question? @SushiHangover –  Apr 04 '19 at 07:46
  • are you closing any existing connections prior to the copy? – Jason Apr 04 '19 at 09:03
  • No, Do I need to close it? –  Apr 04 '19 at 09:10

2 Answers2

0

If you have your database on android device - it could be very easy using Android Studio (if you have it on your PC):

  1. Open Android Studio, click View->Tool Windows -> Device File Explorer;

  2. In th opened window, go to your app location and find file *.db

    enter image description here

  3. Click to 'Save as' and save to your PC.

Also I can recommend a very useful app for checking your sqlite database - DB Browser for SQLite. Try it!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

You can extract the .db file from the apk. To do that first you need to create a backup from your application. Connect the Device with USB and use the adb to backup your app with command: adb backup -f ~/yourapp.ab -noapk com.yourname.yourapp

That'll create a .ab (Android Backup) file in your home directory. You can extract the .db file out of that .ab file with another command:

dd if=data.ab bs=1 skip=24 | openssl zlib -d | tar -xvf -