为了账号安全,请及时绑定邮箱和手机立即绑定

c# android中是否存在记录如何检查Sqlite

c# android中是否存在记录如何检查Sqlite

C#
守着一只汪 2022-10-23 10:09:15
我有 c# android 应用程序。我有一个表单,其中包含一些字段 jobno、路线、成本,当用户填写表单并按保存时,信息将存储在应用程序的 SQLite 数据库中。我想做的是在 if 检查记录是否存在时添加一个 if 语句;如果没有添加条目,则不添加记录。这是用户单击保存按钮时的代码。    Record photo = new Record()    {        Jobno = jobno.Text,        route= route.Text,        cost= cost.Text,        Timestamp = timestamp.Timestamp,    };    using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DB_PATH))    {        //check if records exist        conn.CreateTable<Record>();        var numberofrows = conn.Insert(record);        if (numberofrows > 0)        {            DisplayAlert("Success", "record has been saved successfully", "Ok");            MainImage.Source = " ";        }        else        {            DisplayAlert("Failure", "Error occoured while saving record", "Try again");        }    }如何添加 if 语句以仅在不存在时添加新记录。提前致谢编辑问题    using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DB_PATH))    {        //check if records exist        conn.CreateTable<Record>();        var items = conn.Table<Record>().Where(array => array.Jobno == photo.Jobno && array.Applicationletter == photo.Applicationletter && array.Signno == photo.Signno);        if (items == null)        {            var numberofrows = conn.Insert(photo);            if (numberofrows > 0)            {                DisplayAlert("Success", "record has been saved successfully", "Ok");                MainImage.Source = " ";            }            else            {                DisplayAlert("Failure", "Error occoured while saving record", "Try again");            }        }        else        {            DisplayAlert("Failure", "Photo already exist", "ok");        }        //clear the notes field        notesentry.Text = "";    }
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

试试这个方法。我不确定你的 sqlite 代码是如何工作的,所以我只是在这里猜测。我还猜测您想检查新创建的记录是否存在,因此此代码基于此。


conn.CreateTable<Record>();

//check if records exist

var items = conn.Table<Record>().Where(array => array.Jobno == photo.Jobno && array.Applicationletter == photo.Applicationletter && array.Signno == photo.Signno);

if (items?.Count() == 0)

{

   var numberofrows = conn.Insert(record);


   if (numberofrows > 0)

   {

      DisplayAlert("Success", "record has been saved successfully", "Ok");

      MainImage.Source = " ";

   }

   else

   {

      DisplayAlert("Failure", "Error occoured while saving record", "Try again");

   }

}

else

{

       DisplayAlert("Failure", "Photo already exist", "ok");

}


        //clear the notes field

        notesentry.Text = "";


查看完整回答
反对 回复 2022-10-23
  • 1 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信