James Hickling
James Hickling

Reputation: 119

How to check Sqlite if record exist in c# android

I have c# android application. I have a form with some fields jobno, route, cost and when user fill the form and press save, the information gets stored in SQLite database in app.

What I would like to do is add an if statement to if check if the record exist; add an entry if not don't add a record.

This is my code when user click save button.

    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");
        }
    }

How to add if statement to only add a new record if don't exist.

Thanks in advance

Edit question

    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 = "";
    }

Upvotes: 1

Views: 1055

Answers (1)

JayHandle
JayHandle

Reputation: 186

Try this method. I'm not sure how your sqlite code works so im just guessing here. I am also guessing that you want to check if the newly created record exist, so this code is based off that.

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 = "";

Upvotes: 3

Related Questions