使用Xamarin + C#開發應用 -- 使用sqlite做本地存儲
1. 下載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();
if (!db.Table().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();
foreach (var s in table)
{
sb.Append(s.Id + " " + s.Symbol);
}
return sb.ToString();