應用程序應該採取一種成分,並搜索數據庫,並返回像卡路里等東西。我只是從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
1)你爲什麼要調用CreateTable,如果你已經有一個數據庫在其中的數據庫? 2)當查詢文本字段時,您需要在查詢參數周圍放置。 3)您是否嘗試過統計表中的項目以驗證它們是否符合您的期望? – Jason
1)我找不到任何關於查詢數據庫的事情,因爲我剛剛接觸它,我試圖根據其他示例進行查詢。如果我只是做「var table = db.Table();」,我會得到同樣的結果。 2)如果我把''放在str1中,它會給出錯誤。 3)是的,我數了他們,它應該是正確的。 –
Steve
如果您有一個現有的帶有表格的數據庫,則無需創建表格。如果您發佈一個鏈接到您的代碼,我會看看它 – Jason