-
Notifications
You must be signed in to change notification settings - Fork 8
SQLite on iOS
Philippe Leybaert edited this page Aug 10, 2016
·
6 revisions
To use Iridium with SQLite in Xamarin.iOS, you only need to add the Iridium library to your app (available as a Nuget package). No other libraries are required.
var dbName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "my.db";
var dbContext = new SqliteContext(dbName);
// ...
dbContext.CreateTable<Customer>();
//
dbContext.DataSet<Customer>().Save(new Customer { Name = "John" });
//
var johnCustomers = from c in dbContext.DataSet<Customer>()
where c.Name.StartsWith("John")
select c;
public class DbContext : SqliteContext
{
public DbContext() : base(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "my.db")
{
}
public IDataSet<Customer> Customers {get;set;}
public IDataSet<Product> Products {get;set;}
}
// ...
var dbContext = new DbContext();
//
dbContext.Customers.Save(new Customer { Name = "John" });
//
var johnCustomers = from c in dbContext.Customers
where c.Name.StartsWith("John")
select c;
You can also set the name of the database file later if you prefer (as long as you set it before the first data access):
public class DbContext : SqliteContext
{
public IDataSet<Customer> Customers {get;set;}
public IDataSet<Product> Products {get;set;}
}
// ...
var dbContext = new DbContext();
// ...
dbContext.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "my.db");