0
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