Sponser Link

Insert query in SQLite Xamarin forms

In previous tutorial, we have learnt that how to create database of SQLite, now we will Insert the data in table.

You will create a new instance of employee class and will be set values of attributes. Firstly you will go to database class and will write the given below method.

public Task SaveEmployeeAsync(Employee emp)
{
   return _db.InsertAsync(emp);
}

In above method, the employee object will be passed and function will return the no of rows inserted. Now go to MainPage.xaml.cs and write the new function of save and call the save employee function which is written in database class.

 public void SaveRegister(string str_user)
   {
            Employee emp = new Employee();
           
            emp.EmpID= txtEmp.Text;
            emp.EmpNo = txtEmp.Text;
            emp.UserName = str_user;
            emp.DeviceID= "test";
            emp.IsActive = 0;
            emp.IsRegister = 1;

            App.EmpDB.SaveEmployeeAsync(emp);

    }

Above function will save the data in database, you can perform any operation on this employee table.