locked
Loading a Sqlite db file embedded as a ressource file RRS feed

  • Question

  • User142042 posted

    Can I, directly from PCL code in a XF app, open up a SQLite file using GetManifestResourceStream ? Or do I need to first copy the file in the user's space in each platform prior to open it (hence using dependency service) ? The SQLite DB is intended to be exclusively read-only!

    Thanks

    Thursday, July 21, 2016 5:01 PM

Answers

  • User2148 posted

    Sorry... I think yes. In this sample use a SQLite DB, and it's under Resource...

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, July 21, 2016 7:33 PM

All replies

  • User2148 posted

    I think you have to move the file from Assets to a directory

    I think something like this

    // Ripristino sempre il file di db
    string DB_ANAGRAFICA_FILENAME = "GECO.sqlite";
    string folderPath = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
    string dbPath = Path.Combine ( folderPath, DB_ANAGRAFICA_FILENAME);
    //Environment.GetFolderPath (Environment.SpecialFolder.Personal)
    
    if(System.IO.File.Exists(dbPath))
        System.IO.File.Delete(dbPath);
    
    
    using (BinaryReader br = new BinaryReader(Assets.Open(DB_ANAGRAFICA_FILENAME)))
    {
        using (BinaryWriter bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
        {
            byte[] buffer = new byte[2048];
            int len = 0;
            while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
            {
                bw.Write (buffer, 0, len);
            }
        }
    }
    
    Thursday, July 21, 2016 6:20 PM
  • User142042 posted

    Hey, thanks for the quick answer, but I do not wish the file (SQLite file) to be an asset, but an embedded resource. If it were an asset, you are entirely right that I need to copy it first, cause it wouldn't be accessible otherwise.

    My use case is one that would use the data in the database to load essential data to the app, but remains read only. If it were read write, I would simply load the file as an embedded resource and then copy it in the proper folder on each platform, as it is explained here by @JonathanPryor https://forums.xamarin.com/discussion/4238/how-can-i-access-an-already-existing-sqlite-database-in-mono-for-android

    But his exemple doesn't mention if I could forego with copying the file, since I only need it for read access!

    Thursday, July 21, 2016 7:00 PM
  • User2148 posted

    Sorry... I think yes. In this sample use a SQLite DB, and it's under Resource...

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, July 21, 2016 7:33 PM
  • User142042 posted

    @AlessandroCaliaro Thanks!

    Thursday, July 21, 2016 7:39 PM
  • User30212 posted

    @AlessandroCaliaro The link above is not working. Could you share the sample?

    Thursday, January 19, 2017 11:44 PM
  • User322420 posted

    I use that solution to open .mbtiles file that added to .NET Standard project as EmbeddedResource. It works on Android and iOS. Thanks @AlessandroCaliaro for his answer:

        var embeddedResourceDb = Assembly.GetExecutingAssembly().GetManifestResourceNames().First(s => s.Contains("world.mbtiles"));
                    var embeddedResourceDbStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedResourceDb);
    
                    // Load tiles data from bundle to phone cache on first launch
                    var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "world.mbtiles");
                    if (!File.Exists(dbPath))
                    {
                        using (var br = new BinaryReader(embeddedResourceDbStream))
                        {
                            using (var bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
                            {
                                var buffer = new byte[2048];
                                int len;
                                while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    bw.Write(buffer, 0, len);
                                }
                            }
                        }
                    }
    
    Wednesday, April 4, 2018 6:05 AM