I have database and i want to import it in my project ,first i added a SQLITe component to my project and insert my DB to ASSETS folder.
All the examples that i have seen it's about how to create database in project, but i already have the database and i want to make some query to display it in my app.
I checked this tutorial: Use a local database in Xamarin
But it does not work!
This my code:
void conndb ()
{
string dbName = "Chinook_Sqlite.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);
}
}
}
using (var conn = new SQLite.SQLiteConnection(dbPath))
{
var cmd = new SQLite.SQLiteCommand (conn);
cmd.CommandText = "select * from Album where AlbumId=1;";
var r = cmd.ExecuteQuery<Album> ();
Console.Write (r);
}
}
}
Any help?