繁体   English   中英

Android sqlite查询什么都不返回

[英]Android sqlite query returning nothing

App应该采用某种成分并搜索数据库,然后返回卡路里等东西。 我只是从Assets中打开文件并进行了复制,打开了副本并创建了一个表(据称;我在它们上尝试了catch(异常),并说它们成功了)。 但是我尝试运行一个查询,它给出一个错误“无法将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; }
        }
    }
}

资产文件中肯定有数据: 在此处输入图片描述

您的代码有很多问题。 这些是亮点:

  1. 您资产文件夹中的文件不是SQLite数据库

  2. 数据库中的表名(一旦修复#1)为'USDADataProto',而不是'usdProto'。 您正在查询不存在的表。

  3. 您的映射类应使用正确的表名“ USDADataProto”

  4. 您的数据全部为大写,因此在查询时需要考虑到这一点

像这样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM