vendredi 17 avril 2015

how to import database and use it locally in xamarin?

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 ASSEST 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 check 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 helps ?


Aucun commentaire:

Enregistrer un commentaire