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; }
可能な複製フォルダ](http://stackoverflow.com/questions/20857734/reading-sqlite-file-from-asset-folder) – SushiHangover
どのような例外とスタックトレースを取得しますか? – Cheesebaron
現在のところ、何らかの理由で私のデバッグが機能していないため、「データベースを作成できませんでした」という例外が表示されています。しかし、それはまったく別のものです。 – Steve