Home > Back-end >  Export SQLite database to phone folders
Export SQLite database to phone folders

Time:03-07

I am trying to collect data on an Android Phone, for which I am writing an app. The data is succesfully stored on a local SQLite database, but I am having great difficulty with exporting the SQLite database to a file that I can access from the phone itself. I have asked for both Read and Write permission on Android and they are granted. I have also tried to copy the database to different folders, without success.

private void ExportData(object sender, EventArgs e)
{
    File.Copy(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "database.db3"), 
           Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "allData.db3"));
}

How can I copy my .db3 file to accessible folders on the Android Device?

CodePudding user response:

This is how to export data from the app storage onto your SD-card:

private void ExportData(object sender, EventArgs e)
{
    string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "databasename.db3");
    var bytes = File.ReadAllBytes(path);
    var fileCopyName = string.Format("/sdcard/Database_{0:dd-MM-yyyy_HH-mm-ss-tt}.db", DateTime.Now);
    File.WriteAllBytes(fileCopyName, bytes);
}

It requires write permission in your AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • Related