0

New to Xamarin and c#. Trying to make an app that searches a database for an ingredient and returns nutritional info. I've read the guides and searched for quite a while, but no matter what I do I cannot make this work. What am I doing wrong here? My database in in my assets folder and it definitely has data in it. Currently when I run it it just crashes at the copyDatabase() call. If I comment that out it runs but I'm not getting any errors.

using System;
using Android.Views;
using Android.Content;
using Android.Runtime;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content.Res;
using System.IO;
using SQLite;
using System.Linq;
using Android.Database.Sqlite;

namespace nutr_grabber
{
[Activity(Label = "nutr_grabber", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    string str1;

    private static string DB_PATH = "/data/data/nutr_grabber/databases/";

    private static string DB_NAME = "UsdDataProto.db";

    private void copyDataBase()
    {
        var dbInput = ApplicationContext.Assets.Open(DB_NAME);
        string dbProto = DB_PATH + DB_NAME;
        var myOutput = new FileStream(dbProto, FileMode.OpenOrCreate);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = dbInput.Read(buffer, 0, 1024)) > 0)
            myOutput.Write(buffer, 0, length);
        myOutput.Flush();
        myOutput.Close();
        dbInput.Close();
    }

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        copyDataBase();
        // Set our view from the "main" layout resource

        SetContentView(Resource.Layout.Main);

        //set widgets
        TextView message = FindViewById<TextView>(Resource.Id.message);
        EditText ingred = FindViewById<EditText>(Resource.Id.enterHere);
        Button search = FindViewById<Button>(Resource.Id.search);

        search.Click += (object sender, EventArgs e) =>
        {
            str1 = ingred.Text;

           string usd = DB_PATH+ DB_NAME;
            using (var conn = new SQLite.SQLiteConnection(usd))
            {
                conn.CreateTable<usdProto>();

                 var Item = conn.Table<usdProto>().Where(v => v.Shrt_Desc.Contains(str1));
                new AlertDialog.Builder(this)
             .SetMessage(Item)
             .Show();
            }
        };
    }
}

public class usdProto
{
    [PrimaryKey]

    public int NDB_No { get; set; }
    public string Shrt_Desc { get; set; }
    public int Energ_Kcal { get; set; }
    public int Protein_g { get; set; }
    public int Lipid_Tot_g { get; set; }
    public int Ash_g { get; set; }
    public int Carbohydrt_g { get; set; }
    public int Fiber_TD_g { get; set; }
    public int Sugar_Tot_g { get; set; }
    public int Calcium_mg { get; set; }
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
Steve
  • 11
  • 5
  • Possible duplicate of [Reading sqlite file from asset folder](http://stackoverflow.com/questions/20857734/reading-sqlite-file-from-asset-folder) – SushiHangover Oct 04 '16 at 04:45
  • What exception and stack trace do you get? – Cheesebaron Oct 04 '16 at 14:18
  • I currently just have an exception that displays " couldn't create database" because for whatever reason, my debug isn't working. But that' a whole other thing. – Steve Oct 04 '16 at 15:30
  • Is that definitely the correct path to your Db? – jaymarvels Oct 04 '16 at 15:37
  • It's supposed to copy the database from the file in the assets folder. From what i understand that's the best way to do it. – Steve Oct 04 '16 at 15:48

1 Answers1

0

I'd using something along the lines of this:

        var path = DB_PATH + DB_NAME;
        using (var stream = (Assets.Open(DB_NAME)))
        using (var filestream = File.Create(path, (int)stream.Length))
        {
            var byteInstream = new byte[2048];
            int i;
            while ((i = stream.Read(byteInstream, 0, byteInstream.Length)) > 0)
            {
                filestream.Write(byteInstream, 0, i);
            }
        }

Make sure also that you have the correct permissions set in your manifests

jaymarvels
  • 436
  • 1
  • 8
  • 21