Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin Android C SQLite参数查询_C#_Android_Sqlite_Xamarin - Fatal编程技术网

C# Xamarin Android C SQLite参数查询

C# Xamarin Android C SQLite参数查询,c#,android,sqlite,xamarin,C#,Android,Sqlite,Xamarin,我试图根据XamarinAndroidC中的一个参数返回数据。当我将所有数据拉回来时,下面的代码可以正常工作,但是我需要使用SQLite WHERE查询来返回特定的数据 我需要使用AutoCompleteTextView字段中的值作为参数 protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set

我试图根据XamarinAndroidC中的一个参数返回数据。当我将所有数据拉回来时,下面的代码可以正常工作,但是我需要使用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;
    }

如何在查询中使用参数

假设您使用的是我使用的Sqlite net pcl中的一个Sqlite net包变体,那么您将创建一个字符串数组,其中的参数按照在标记为via?的SQL查询中被替换的顺序排列

在本例中,从我的一个应用程序中直接拉出来的是在方法中传递两个参数,一个字符串和一个bool

需要转换0或1的字符串表示形式,因为布尔值在SQLite中存储为整数

另一个将删除空白,我将SQL字符串通配符%添加到字符串的开头和结尾

我用这两个变量创建一个字符串数组,我的SQL语句包含两个?将替换为在SQL查询语句中按左2右顺序找到的参数

实例
您的实际问题是什么?如何在查询中使用参数?我无法确定是否是这种情况,但如果您在任何地方使用SqliteCommand,请使用参数集合属性。DbHelperHanks这真的很有用,我假设我可以很容易地将AutoCompleteTextView中的值作为参数?@WHoward绝对,我的示例来自基于Xamarin.Android的SearchView,并将用户的搜索条目传递给此方法以过滤RecyclerView,在您的情况下,您将获取返回的IList,并将其作为自动完成条目下的下拉列表呈现给用户,假设这是您的最终目标。在某种程度上,用户将输入选择的国家,并基于此,与之相关的数据将返回/显示给用户。再次感谢
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);
        }
    }
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;
}