Xamarin / Visual Studio 2015 - 我应该使用sqlite和查询问题,

时间:2016-09-30 17:10:03

标签: android sqlite xamarin

Xamarin和c#的新手,但是我试图为我的班级制作一个非常简单的应用程序,在数据库中搜索成分并返回营养信息。我的小组知道python,所以他们写了代码,我试着制作一个简单的应用程序,因为我可以只是为了节目,但我有一些麻烦。我们从他们的网站下载了USDA数据库,并且该文件中的数据位于

" NDB_No,Shrt_Desc,Energ_Kcal,Protein_g,Lipid_Tot_g,Ash_g,Carbohydrt_g,Fiber_TD_g,Sugar_Tot_g,Calcium_mg,Iron_mg,Magnesium_mg,Phosphorus_mg,Potassium_mg,Sodium_mg,Zinc_mg,Copper_mg,Manganese_mg,Selenium_ug,Vit_C_mg,Thiamin_mg,Riboflavin_mg ,Niacin_mg,Panto_Acid_mg,Vit_B6_mg,Folate_Tot_ug,Folic_Acid_ug,Food_Folate_ug,Folate_DFE_ug,Choline_Tot_毫克,Vit_B12_ug,Vit_A_IU,Vit_A_RAE,Retinol_ug,Alpha_Carot_ug,Beta_Carot_ug,Beta_Crypt_ug,Lycopene_ug,甩+ Zea_微克,Vit_E_mg,Vit_D_ug,Vit_D_IU,Vit_K_ug,FA_Sat_g,FA_Mono_g ,FA_Poly_g,Cholestrl_mg,Gm_unit,NUM,单元"

具有数百种成分的格式。如

" 1145,黄油,717,0.85,81.11,0.09,0.06,0,0.06,24,0.02,2,24,24,11,0.09,0.016,0.004,1,0,0.005,0.034 ,0.042,0.11,0.003,3,0,3,3,18.8,0.17,2499,684,671,0,158,0,0,0,2.32,0,0,7,50.489,23.43,3.01,215,5,1 ,汤匙"

他们使用sqlite3创建一个表并在Python中查询它,只需使用c.fetchrow返回一行数据和一个数组来分割它。该代码可在此处找到:https://github.com/pjomara/SoftwareEngineeringIIProject/blob/master/nutr_data_grabber.py

我找不到任何好的例子,也不需要创建数据库,只需从我的数据库中读取即可。 我被困了,任何帮助,示例代码等都会很棒。感谢

目前的错误是" 错误CS1503参数1:无法转换为' SQLite.TableQuery'到' int' nutr_grabber C:\ Users \ Steve \ Documents \ Visual Studio 2015 \ Projects \ nutr_grabber \ nutr_grabber \ MainActivity.cs 53 Active"

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

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

        string proto = DB_PATH + DB_NAME;
        //string folder = System.Environment.GetFolderPath();
        var db = new SQLiteConnection(System.IO.Path.Combine( proto , "UsdDataProto.db"));
        db.CreateTable<usdProto>();



        search.Click += (object sender, EventArgs e) =>
        {
            str1 = ingred.Text;



            var Item = db.Query<usdProto>("SELECT Energ_Kcal FROM usdProto WHERE Shrt_Desc = ?", 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; }

1 个答案:

答案 0 :(得分:0)

你的查询将返回一个IEnumerable对象,在你的情况下usdProto:

var query = db.Table<usdProto>().Where(v => v.Shrt_Desc.Contains(str1));

foreach (var item in query) {
  new AlertDialog.Builder(this)
            .SetMessage(item.SomeProperty)
            .Show();
}

将someProperty替换为您要显示的usdProto属性