標籤:
1. 下載sqlite.cs:
https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs
2. 建立 測試實體:
[Table("Items")] public class Stock { [PrimaryKey, AutoIncrement, Column("_id")] public int Id { get; set; } [MaxLength(8)] public string Symbol { get; set; } }
3. 樣本
3.1 定義路徑
3.2 建立串連
3.3 建立表
3.4 插入實體
3.5 列印結果
範例程式碼如下:
string dbPath = string.Format("{0}//{1}", Environment.GetFolderPath(Environment.SpecialFolder.Personal), "ormdemo.db3"); var db = new SQLiteConnection(dbPath); db.CreateTable<Stock>(); if (!db.Table<Stock>().Any()) { // only insert the data if it doesn‘t already exist var newStock = new Stock { Symbol = "AAPL" }; db.Insert(newStock); newStock = new Stock { Symbol = "GOOG" }; db.Insert(newStock); newStock = new Stock { Symbol = "MSFT" }; db.Insert(newStock); } //Console.WriteLine("Reading data"); var sb = new StringBuffer(); var table = db.Table<Stock>(); foreach (var s in table) { sb.Append(s.Id + " " + s.Symbol); } return sb.ToString();
使用Xamarin + C#開發應用 -- 使用sqlite做本機存放區