0

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?

Community
  • 1
  • 1
user3348242
  • 41
  • 1
  • 5
  • 1
    "but it does not work" - what does this mean? Do you get errors or exceptions? Does it not compile? – Jason Apr 17 '15 at 13:29
  • yes i can build and compile my app but when run it into my emulator my app crashed – user3348242 Apr 17 '15 at 14:11
  • 1
    Well, what line did it crash on? Have you attempted to debug it to trace the error? – Jason Apr 17 '15 at 14:13
  • i added try to this method and gives me an exception...how can i trace it ? – user3348242 Apr 17 '15 at 14:19
  • 1
    What exception did you get? Have you tried stepping through your code line-by-line in the debugger to find the specific line causing the exception? You can also look at the stack trace in the exception object to find the source of the error. – Jason Apr 17 '15 at 14:24
  • Author of that tutorial here, you need to give us the exception and code line(s) where the crash occurs. Can't do much without the specifics of the error. – matthewrdev Apr 19 '15 at 21:55
  • thanks so much ..i solve it ...this code is fine but the problem is i didn't select the permission of read and write of external storage . – user3348242 Apr 20 '15 at 18:36

1 Answers1

0

You will need to upload your SQLite DB into your Android Device using the File Explorer. You will need to use Android Studio, I do not believe Xamarin Studio has the equivalent tool.

If you start Monitor from the Android Studio (green robot icon on the toolbar, to the left of the help icon) you can then can select File Explorer tab. You should be able to upload your DB File to the location you're calling in your Xamarin C# Code.

Eddie
  • 466
  • 5
  • 8