具有多个WHERE条件的SQLite数据库查询

时间:2014-07-06 16:22:24

标签: c# database sqlite sqlite-net

尝试开发Windows 8.1商店应用。除了其他困难之外,我还需要在where子句中使用两个参数从sqlite数据库中检索记录。我可以使用where子句中的一个参数成功查询,但是当我尝试使用两个参数时它会崩溃。这是我的代码:

    public string SaveMaint(MaintViewModel Maintenance)
    {
        string result = string.Empty;
        using (var db = new SQLite.SQLiteConnection(App.DBPath))
        {
            string change = string.Empty;
            try
            {
                var existingmaintenance = db.Query<maintenance> ("select * from maintenance where VehID = ? AND MaintID = ?", new String[] {Maintenance.Maintid, Maintenance.Vehicleid});
               // var existingmaintenance = (db.Table<maintenance>().Where 
               //     (c => c.MaintID == Maintenance.Maintid).SingleOrDefault());

                if (existingmaintenance != null)
                {
                    existingmaintenance.VehID = Maintenance.Vehicleid;
                    existingmaintenance.MaintID = Maintenance.Maintid; 
                    existingmaintenance.ServiceDate = Maintenance.Servicedate;
                    existingmaintenance.ServiceCost = Maintenance.Servicecost;
                    existingmaintenance.ServiceLocation = Maintenance.Servicelocation;
                    existingmaintenance.ServiceNote = Maintenance.Servicenote;
                    existingmaintenance.ServiceOdom = Maintenance.Serviceodom;
                    int success = db.Update(existingmaintenance);
                }
                else
                {
                    int success = db.Insert(new maintenance()
                    {
                        VehID = Maintenance.Vehicleid,
                        MaintID = Maintenance.Maintid,
                        ServiceDate = Maintenance.Servicedate,
                        ServiceCost = Maintenance.Servicecost,
                        ServiceLocation = Maintenance.Servicelocation,
                        ServiceNote = Maintenance.Servicenote,
                        ServiceOdom = Maintenance.Serviceodom
                    });
                }
                result = "Success";
            }
            catch
            {
                result = "This project was not saved.";
            }
        }
        return result;
    }

请参阅定义existingmaintenance变量的行。这行的注释版本工作正常。当我用两个参数查询替换变量定义时(使用不同的方法获得,因为我无法弄清楚如何向Table查询方法添加第二个参数),它会崩溃。

感谢您提供任何帮助。对不起,我只知道自己在做什么。

2 个答案:

答案 0 :(得分:12)

假设您使用SQLite-Net作为ORM,您可以在查询后传递参数。据我所知,没有像你的例子那样支持匿名类。试试这个:

var existingmaintenance = db.Query<maintenance>(
    "select * from maintenance where VehID = ? AND MaintID = ?",
    Maintenance.Vehicleid, Maintenance.Maintid).FirstOrDefault();

您也可以使用linq查询,如下所示:

var existingmaintenance = db.Table<maintenance>().Where 
    (c => c.VehID == Maintenance.Vehicleid && 
    c.MaintID == Maintenance.Maintid).FirstOrDefault();

答案 1 :(得分:0)

试试这个

我希望这两个Id都是数据库中的int

var existingmaintenance = db.Query<maintenance> ("select * from maintenance where VehID = " + Maintenance.Maintid  + " AND MaintID = " + Maintenance.Vehicleid ).FirstOrDefault();