public SpatialiteConnection GetSpatialiteConnection(string dbPath, string prepackagedDatabaseName = null, bool overrideIfPrepackagedExists = false) { //first move the pre-packaged database if it doesnt exist already or if overrideRequired if (!string.IsNullOrEmpty(prepackagedDatabaseName) && (!File.Exists(dbPath) || overrideIfPrepackagedExists)) { if (File.Exists(dbPath)) { File.Delete(dbPath); } var existingDb = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(prepackagedDatabaseName), Path.GetExtension(prepackagedDatabaseName)); File.Copy(existingDb, dbPath); } if (!SpatialiteInfo.BatteriesInit) { SQLitePCL.Batteries.Init(); SpatialiteInfo.BatteriesInit = true; } //different library names based on arch string x86_64 = "mod_spatialite_x86_64.7.dylib"; string arm64 = "mod_spatialite_arm64.7.dylib"; bool isSimulator = IsSimulator(); string libPath = new FileInfo($"./Frameworks/iOSSpatialite.framework/Frameworks/{(isSimulator ? x86_64 : arm64)}") .FullName; SQLiteConnection sqliteConnection = new SQLiteConnection(dbPath); //getting reference to raw db to enable loading extensions sqliteConnection.EnableLoadExtension(true); string query = $"select load_extension('{libPath}','sqlite3_modspatialite_init');"; try { sqliteConnection.Query <object>(query); SpatialiteConnection spatialiteConnection = new SpatialiteConnection { SQLiteConnection = sqliteConnection }; return(spatialiteConnection); } catch (Exception e) { throw; } }
public SpatialiteConnection GetSpatialiteConnection(string dbPath, string prepackagedDatabaseName = null, bool overrideIfPrepackagedExists = false) { //first move the pre-packaged database if it doesnt exist already or if overrideRequired if (!string.IsNullOrEmpty(prepackagedDatabaseName) && (!File.Exists(dbPath) || overrideIfPrepackagedExists)) { DeletePreviousDatabase(dbPath); MoveDatabase(dbPath, prepackagedDatabaseName); } if (!SpatialiteInfo.BatteriesInit) { SQLitePCL.Batteries.Init(); SpatialiteInfo.BatteriesInit = true; } SQLiteConnection sqliteConnection = new SQLiteConnection(dbPath); sqliteConnection.EnableLoadExtension(true); //enable extension via loading module through sqlite select and specifying entry point //as android rule makes library name to start with lib //and if not specified entry point, sqlite will "guess" the entry point using name try { sqliteConnection.Query <object>("select load_extension('libspatialite.so','sqlite3_modspatialite_init');"); SpatialiteConnection spatialiteConnection = new SpatialiteConnection { SQLiteConnection = sqliteConnection }; return(spatialiteConnection); } catch (Exception e) { throw; } }