Book Image

iOS Development with Xamarin Cookbook

By : Dimitrios Tavlikos (USD)
Book Image

iOS Development with Xamarin Cookbook

By: Dimitrios Tavlikos (USD)

Overview of this book

Table of Contents (22 chapters)
iOS Development with Xamarin Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using a SQLite database


In this recipe, we will learn how to create a SQLite database file. We will create a table, insert some data into it, and then query the table to display the data on screen.

Getting ready

Create a new iPhone Single View Application in Xamarin Studio and name it SQLiteApp. Add three buttons and a label on the view controller. Do not forget to connect them to the outlets.

How to do it...

Perform the following steps:

  1. Add references to the assemblies Mono.Data.Sqlite and System.Data. The following screenshot shows how to add a reference to the project:

  2. For creating the database and the table, enter the following method in the SQLiteAppViewController class:

    private void CreateSQLiteDatabase (string databaseFile)
    {
      try
      {
        if (!File.Exists (databaseFile))
        {
          SqliteConnection.CreateFile (databaseFile);
          using (SqliteConnection sqlCon = new SqliteConnection (String.Format ("Data Source = {0};", databaseFile)))
          {
            sqlCon.Open ();
            using (SqliteCommand...