App应该采取一种成分并搜索数据库并返回卡路里等内容。我刚刚从Assets打开文件并复制它,打开副本并创建一个表(据说;我在它们上运行try catch(exception)s并且它说它们是成功的)。但我尝试运行一个查询,它给出了一个错误“无法将nutr_grabber.mainactivity.usdProto转换为int”。 如果我尝试使用query.Energ_Kcal,它不会给出任何错误,但根本不会返回任何错误。 有什么想法吗?
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;
// Android needs a databse to be copied from assets to a useable location
public void copyDataBase()
{
var dbPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "UsdDataProto.db");
if (!System.IO.File.Exists(dbPath))
{
var dbAssetStream = Assets.Open("UsdDataProto.db");
var dbFileStream = new FileStream(dbPath, FileMode.OpenOrCreate);
var buffer = new byte[1024];
int b = buffer.Length;
int length;
while ((length = dbAssetStream.Read(buffer, 0, b)) > 0)
{
dbFileStream.Write(buffer, 0, length);
}
dbFileStream.Flush();
dbFileStream.Close();
dbAssetStream.Close();
}
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// makes the database
try
{
copyDataBase();
new AlertDialog.Builder(this)
.SetMessage("Database created ...")
.Show();
}
catch(Exception e)
{
new AlertDialog.Builder(this)
.SetMessage("Database not created ...")
.Show();
}
// 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);
//open sqlite connection, create table
var Path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "UsdDataProto.db");
var db = new SQLiteConnection(Path);
db.CreateTable<usdProto>();
search.Click += (object sender, EventArgs e) =>
{
str1 = ingred.Text;
var query = db.Query<usdProto>("SELECT * FROM usdProto WHERE Shrt_Desc = ?", str1);
foreach (var item in query)
{
new AlertDialog.Builder(this)
.SetMessage(item.Energ_Kcal)
.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; }
public int Iron_mg { get; set; }
public int Magnesium_mg { get; set; }
public int Phosphorus_mg { get; set; }
public int Potassium_mg { get; set; }
public int Sodium_mg { get; set; }
public int Zinc_mg { get; set; }
public int Copper_mg { get; set; }
public int Manganese_mg { get; set; }
public int Selenium_ug { get; set; }
public int Vit_C_mg { get; set; }
public int Thiamin_mg { get; set; }
public int Riboflavin_mg { get; set; }
public int Niacin_mg { get; set; }
public int Panto_Acid_mg { get; set; }
public int Vit_B6_mg { get; set; }
public int Folate_Tot_ug { get; set; }
public int Folic_Acid_ug { get; set; }
public int Food_Folate_ug { get; set; }
public int Folate_DFE_ug { get; set; }
public int Choline_Tot_mg { get; set; }
public int Vit_B12_ug { get; set; }
public int Vit_A_IU { get; set; }
public int Vit_A_RAE { get; set; }
public int Retinol_ug { get; set; }
public int Alpha_Carot_ug { get; set; }
public int Beta_Carot_ug { get; set; }
public int Beta_Crypt_ug { get; set; }
public int Lycopene_ug { get; set; }
public int Lut_Zea_ug { get; set; }
public int Vit_E_mg { get; set; }
public int Vit_D_ug { get; set; }
public int Vit_D_IU { get; set; }
public int Vit_K_ug { get; set; }
public int FA_Sat_g { get; set; }
public int FA_Mono_g { get; set; }
public int FA_Poly_g { get; set; }
public int Cholestrl_mg { get; set; }
public int Gm_unit { get; set; }
public int num { get; set; }
public int unit { get; set; }
}
}
}
资产文件中肯定存在数据: enter image description here
答案 0 :(得分:1)
您的代码存在大量问题。这些是亮点:
资产文件夹中的文件不是SQLite DB
db中的表名(一旦你的修复#1)是'USDADataProto',而不是'usdProto'。您正在查询不存在的表。
您的地图类应使用正确的表名'USDADataProto'
您的数据全部为大写,因此您在查询时需要考虑到这一点
var query = db.Query<USDADataProto>("SELECT * FROM USDADataProto where Shrt_Desc = ?",str1.ToUpper());
foreach (var item in query)
{
new AlertDialog.Builder(this)
.SetMessage(item.Shrt_Desc)
.Show();
}