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; }