我正在尝试根据Xamarin Android C#中的参数返回数据.当拉回所有数据时,我有下面的代码工作,但是我需要使用sqlite WHERE查询返回特定数据.
我需要使用autoCompleteTextVIEw字段中的值作为参数.
protected overrIDe voID OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our vIEw from the "main" layout resource //Actionbar.NavigationMode = ActionbarNavigationMode.Tabs; SetContentVIEw(Resource.Layout.Main); db = new DBHelper(this); sqliteDB = db.WritableDatabase; container = FindVIEwByID<linearLayout>(Resource.ID.container); var btnEmergencyServices = FindVIEwByID<button>(Resource.ID.btnEmergencyServices); btnEmergencyServices.Click += btnEmergencyServices_onClick; }
private voID EmergencyServicesData() { ICursor selectData = sqliteDB.Rawquery("select POliCE, FIRE, MEDICAL from EmergencyServices WHERE COUNTRY = @Country", new string[] { }); if (selectData.Count > 0) { selectData.MovetoFirst(); do { EmergencyServices emergencyServices = new EmergencyServices(); emergencyServices.POliCE = selectData.GetString(selectData.GetColumnIndex("POliCE")); emergencyServices.FIRE = selectData.GetString(selectData.GetColumnIndex("FIRE")); emergencyServices.MEDICAL = selectData.GetString(selectData.GetColumnIndex("MEDICAL")); EmergencyServices.Add(emergencyServices); } while (selectData.MovetoNext()); selectData.Close(); } foreach (var item in EmergencyServices) { LayoutInflater layoutInflater = (LayoutInflater)BaseContext.GetSystemService(Context.LayoutInflaterService); VIEw addVIEw = layoutInflater.Inflate(Resource.Layout.EmergencyServices, null); TextVIEw txtPolice = addVIEw.FindVIEwByID<TextVIEw>(Resource.ID.txtPolice); TextVIEw txtFire = addVIEw.FindVIEwByID<TextVIEw>(Resource.ID.txtFire); TextVIEw txtMedical = addVIEw.FindVIEwByID<TextVIEw>(Resource.ID.txtMedical); txtPolice.Text = item.POliCE; txtFire.Text = item.FIRE; txtMedical.Text = item.MEDICAL; container.AddVIEw(addVIEw); } }
解决方法:
How do I use a parameter with my query?
假设您使用的是sqlite net程序包变体之一(我使用sqlite-net-pcl),则创建一个带有参数的字符串数组,以便在SQL查询中替换它们(用?标记).
在此示例中,直接从我的一个应用程序中抽出,并在方法中传递了两个参数:字符串和布尔值.
由于布尔值以整数形式存储在sqlite中,因此需要转换0或1的字符串表示形式.
另一个被修剪空白,我将sql字符串通配符%添加到字符串的开头和结尾.
我用这两个变量创建一个字符串数组,而我的SQL语句包含两个?它将以在SQL查询语句中找到的参数从左到右的顺序替换为这些参数.
例
public async Task<IList<Package>> DbGetSearchedPackagesAsync(string constraint, bool isUserApp = true){ var param1 = Convert.ToInt32(isUserApp).ToString(); var param2 = $"%{constraint.Trim()}%"; var packages = await conn.queryAsync<Package>("SELECT * FROM Package WHERE UserApp = ? AND name liKE ? ORDER BY name ColLATE NOCASE ASC;", new string[2] { param1, param2 }); return packages;}
总结 以上是内存溢出为你收集整理的Xamarin Android C#SQLite参数查询全部内容,希望文章能够帮你解决Xamarin Android C#SQLite参数查询所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)