Search code examples
iphonesqliteios6xamarin

Xamarin IOS - how to include the existing sqlite database file into solution


In Xamarin IOS solution, the standard code for sqlite database path is:

var sqliteFilename = "MyDatabase.db3";        
string libraryPath = Path.Combine (documentsPath, "..", "Library"); // Library folder instead
var path = Path.Combine (libraryPath, sqliteFilename);

The above code is fine if the database is created from scratch. but my question is how should I include the existing sqlite database into the solution, so it can be copied over to "Library" folder?


Solution

  • Include your database as a file in your solution, marking it as "Build Action -> Content". In your code, you can access it relative to the root of your project (ie, "/mydb.db3") - then read it and copy it to your Library or wherever you want to place it.

    Keep in mind that files bundled into your application are READ-ONLY. You will have to copy the db to a writable path before you can write to it.