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.
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.
Perform the following steps:
Add references to the assemblies
Mono.Data.Sqlite
andSystem.Data
. The following screenshot shows how to add a reference to the project: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...