打开现有数据库崩溃

时间:2016-10-04 04:28:01

标签: android sqlite xamarin xamarin.android

Xamarin和c#的新手。试图制作一个应用程序,在数据库中搜索成分并返回营养信息。我已经阅读了指南并搜索了很长一段时间,但无论我做什么,我都无法完成这项工作。我在这做错了什么?我的数据库在我的资产文件夹中,它肯定有数据。目前,当我运行它时,它只是在copyDatabase()调用时崩溃。如果我评论它运行但我没有收到任何错误。

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

1 个答案:

答案 0 :(得分:0)

我使用了类似的东西:

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

还要确保您在清单中设置了正确的权限