At a glance:
1 – Add Sqlite.cs to your project.
2 – Add your database file as an Asset.
3 – Set your database file to build as an AndroidAsset.
4 – Manually copy the database file out of your apk to another directory.
5 – Open a database connetion using Sqlite.SqliteConnection.
6 – Operate on the database using Sqlite.
1. Add Sqlite.cs to your project.
3. Set DB to build as AndroidAsset.
4. Manually copy DB out of your APK.
string dbName = "db.sqlite"; string dbPath = Path.Combine (Android.OS.Environment.ExternalStorageDirectory.ToString (), dbName); // Check if your DB has already been extracted. if (!File.Exists(dbPath)) { using (BinaryReader br = new BinaryReader(Assets.Open(dbName))) { 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); } } } }
5. Open DB Connection.
using (var conn = new SQLite.SQLiteConnection(dbPath)) { // Do stuff here... }
6. Operate on DB.
References :
http://stackoverflow.com/questions/18715613/use-a-local-database-in-xamarin