In this recipe, we will learn how to write data to the database.
Add two more buttons on the view.
Inside the
CreateSQLiteAppViewController
class, create two methods that will connect to the database file using the code from the previous task. The difference here lies in the usage of theSqliteCommand
object:using (SqliteCommand sqlCom = new SqliteCommand (sqlCon)){ // INSERT statement sqlCom.CommandText = "INSERT INTO Customers (FirstName, LastName) VALUES (@firstName, @lastName)"; sqlCom.Parameters.Add (new SqliteParameter ("@firstName", "John")); sqlCom.Parameters.Add (new SqliteParameter ("@lastName", "Smith")); //UPDATE statement //sqlCom.CommandText = "UPDATE Customers SET FirstName = 'James' WHERE LastName = @lastName"; //sqlCom.Parameters.Add (new SqliteParameter ("@lastName", "Smith")); sqlCom.ExecuteNonQuery (); }