Load existing sqlite DB in Xamarin.iOS

ISUJoeISUJoe Member ✭✭

I've already figured out how to do this on Xamarin.Android, however, I can't figure out how to embed the sqlite database in Xamarin.iOS, then open and read it for consumption inside of my application. Any help would be much appreciated. Currently, I'm using this code, which throws a file not found exception, in which DBFileName is the name of the .sqlite database that's under Resources and set as an embedded resource.

            if (!File.Exists(existingDBFilePath))
            {
                using (var binaryReader = new BinaryReader(File.OpenRead(DBFileName)))
                {
                    using (var binaryWriter = new BinaryWriter(new FileStream(existingDBFilePath, FileMode.Create)))
                    {
                        byte[] buffer = new byte[2048];
                        int length = 0;
                        while ((length = binaryReader.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            binaryWriter.Write(buffer, 0, length);
                        }
                    }
                }
            }

Best Answer

Answers

  • ISUJoeISUJoe Member ✭✭

    A few quick questions:
    1. Should i be using .sqlite or .db files. or does it not matter.
    2. Where in my project folder structure would I store the database I want to copy and open?
    I'm also getting errors:

                var iOSPlatform = new GlobalSQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS();
                _sqLiteDbConnection = new GlobalSQLite.SQLiteConnection(iOSPlatform, path);
    

    where 'Net' does not exist in the namespace of SQLite, and I can't convert from a string to a bool when opening the neq SQLite connection as SQLiteConnection expects (string DatabasePath, [bool storeDataTimeAsTicks=true],[object key=null])

  • fjr_wlb_njfjr_wlb_nj Member ✭✭

    In my database "helper" class I use these properties.

    private static readonly string m_Db_name = "clients_db.sqlite";

    private static readonly string m_folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

    private static readonly string m_Db_path = Path.Combine(m_folderPath, m_Db_name);

    Then in my crud methods I use

    using (var conn = new SQLite.SQLiteConnection(m_Db_path))
    {
    }

    Hope that helps,

Sign In or Register to comment.