Android sqlite查询不返回
问题描述:
应用程序应该采取一种成分,并搜索数据库,并返回像卡路里等东西。我只是从Assets打开文件并复制它,打开副本并创建一个表格(据推测,我对它们运行了try catch(exception),它表示它们已经成功)。但我尝试运行一个查询,它给出了一个错误“不能将nutr_grabber.mainactivity.usdProto转换为int”。 如果我尝试使用query.Energ_Kcal,它不会给出任何错误,但完全不会返回任何结果。 任何想法?Android sqlite查询不返回
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
答
有事情不对您的代码数量巨大。这些都是亮点:
在您的资产文件夹中的文件不是一个SQLite数据库
表名在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();
}
+0
我很生气,因为我一直在使用错误的文件。但它终于有效。非常感谢你,你是一个拯救生命的人。 – Steve
1)你为什么要调用CreateTable,如果你已经有一个数据库在其中的数据库? 2)当查询文本字段时,您需要在查询参数周围放置。 3)您是否尝试过统计表中的项目以验证它们是否符合您的期望? – Jason
1)我找不到任何关于查询数据库的事情,因为我刚刚接触它,我试图根据其他示例进行查询。如果我只是做“var table = db.Table();”,我会得到同样的结果。 2)如果我把''放在str1中,它会给出错误。 3)是的,我数了他们,它应该是正确的。 –
Steve
如果您有一个现有的带有表格的数据库,则无需创建表格。如果您发布一个链接到您的代码,我会看看它 – Jason