继上篇文章后,这里简单介绍下sqlite-net的使用(示例不为作者所写,摘自于:https://github.com/peterhuene/sqlite-net)

Please consult the Wiki for, ahem, complete documentation.

The library contains simple attributes that you can use to control the construction of tables. In a simple stock program, you might use:

public class Stock
{[PrimaryKey, AutoIncrement]public int Id { get; set; }[MaxLength(8)]public string Symbol { get; set; }
}public class Valuation
{[PrimaryKey, AutoIncrement]public int Id { get; set; }[Indexed]public int StockId { get; set; }public DateTime Time { get; set; }public decimal Price { get; set; }
}

Once you've defined the objects in your model you have a choice of APIs. You can use the "synchronous API" where calls block one at a time, or you can use the "asynchronous API" where calls do not block. You may care to use the asynchronous API for mobile applications in order to increase reponsiveness.

Both APIs are explained in the two sections below.

Synchronous API

Once you have defined your entity, you can automatically generate tables in your database by calling CreateTable:

var db = new SQLiteConnection("foofoo");
db.CreateTable<Stock>();
db.CreateTable<Valuation>();

You can insert rows in the database using Insert. If the table contains an auto-incremented primary key, then the value for that key will be available to you after the insert:

public static void AddStock(SQLiteConnection db, string symbol) {var s = db.Insert(new Stock() {Symbol = symbol});Console.WriteLine("{0} == {1}", s.Symbol, s.Id);
}

Similar methods exist for Update and Delete.

The most straightforward way to query for data is using the Table method. This can take predicates for constraining via WHERE clauses and/or adding ORDER BY clauses:

    var conn = new SQLiteConnection("foofoo");var query = conn.Table<Stock>().Where(v => v.Symbol.StartsWith("A"));foreach (var stock in query)Debug.WriteLine("Stock: " + stock.Symbol);

You can also query the database at a low-level using the Query method:

public static IEnumerable<Valuation> QueryValuations (SQLiteConnection db, Stock stock)
{return db.Query<Valuation> ("select * from Valuation where StockId = ?", stock.Id);
}

The generic parameter to the Query method specifies the type of object to create for each row. It can be one of your table classes, or any other class whose public properties match the column returned by the query. For instance, we could rewrite the above query as:

public class Val {public decimal Money { get; set; }public DateTime Date { get; set; }
}
public static IEnumerable<Val> QueryVals (SQLiteConnection db, Stock stock)
{return db.Query<Val> ("select 'Price' as 'Money', 'Time' as 'Date' from Valuation where StockId = ?", stock.Id);
}

You can perform low-level updates of the database using the Execute method.

Asynchronous API

The asynchronous library uses the Task Parallel Library (TPL). As such, normal use of Task objects, and the async and await keywords will work for you.

Once you have defined your entity, you can automatically generate tables by calling CreateTableAsync:

var conn = new SQLiteAsyncConnection("foofoo");
conn.CreateTableAsync<Stock>().ContinueWith((results) =>
{Debug.WriteLine("Table created!");
});

You can insert rows in the database using Insert. If the table contains an auto-incremented primary key, then the value for that key will be available to you after the insert:

    Stock stock = new Stock(){Symbol = "AAPL"};var conn = new SQLiteAsyncConnection("foofoo");conn.InsertAsync(stock).ContinueWith((t) =>{Debug.WriteLine("New customer ID: {0}", stock.Id);});

Similar methods exist for UpdateAsync and DeleteAsync.

Querying for data is most straightforwardly done using the Table method. This will return an AsyncTableQuery instance back, whereupon you can add predictates for constraining via WHERE clauses and/or adding ORDER BY. The database is not physically touched until one of the special retrieval methods - ToListAsync, FirstAsync, or FirstOrDefaultAsync - is called.

    var conn = new SQLiteAsyncConnection("foofoo");var query = conn.Table<Stock>().Where(v => v.Symbol.StartsWith("A"));query.ToListAsync().ContinueWith((t) =>{foreach (var stock in t.Result)Debug.WriteLine("Stock: " + stock.Symbol);});

There are a number of low-level methods available. You can also query the database directly via the QueryAsync method. Over and above the change operations provided by InsertAsync etc you can issue ExecuteAsync methods to change sets of data directly within the database.

Another helpful method is ExecuteScalarAsync. This allows you to return a scalar value from the database easily:

    var conn = new SQLiteAsyncConnection("foofoo");conn.ExecuteScalarAsync<int>("select count(*) from Stock", null).ContinueWith((t) =>{Debug.WriteLine(string.Format("Found '{0}' stock items.", t.Result));});

Special note on use within Windows Store Apps (Windows 8/WinRT)

sqlite-net is fully compliant with WinRT Metro-style apps and will pass Microsoft Store validation.

Please note:

  • Database files will always be created in the path returned by Windows.Storage.ApplicationData.Current.LocalFolder.Path.

  • You will need a copy of sqlite3.dll for your app as well. You can get this from sqlite.org with an installer to the SQLite for Windows Runtime SDK.

转载于:https://www.cnblogs.com/snake-hand/p/3157107.html

windows phone:使用sqlite-net相关推荐

  1. 在windows下查看SQLite数据库

    今天学了<第一行代码>的SQLite数据库,郭老师说用cmd下的adb可以查看,但我的进不了超级用户,没有权限进去(有没有大佬有解决方案?),忧虑中带着感伤,突然找到了其他方法解决,剑走偏 ...

  2. SQLite简介与安装

    SQLite简介: SQLite是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中,实现了自给自足的.无服务器的.零配置的.事务性的 SQL 数据库引擎.就像其他数据 ...

  3. 轻量级数据库Sqlite的使用

    SqLite是什么? SQLite是一个进程内的库,实现了自给自足的.无服务器的.零配置的.事务性的 SQL 数据库引擎.它是一个零配置的数据库,这意味着与其他数据库一样,您不需要在系统中配置. 就像 ...

  4. SQLite 安装(http://www.w3cschool.cc/sqlite/sqlite-installation.html)

    SQLite 安装 SQLite 的一个重要的特性是零配置的,这意味着不需要复杂的安装或管理.本章将讲解 Windows.Linux 和 Mac OS X 上的安装设置. 在 Windows 上安装 ...

  5. 多样化实现Windows Phone 7本地数据访问3——DB4O

    终于把这篇文章发出来了. 对于Windows Phone 7 Visit Local DataBase新采用DB4O和SiaqoDB方式来验证 本地数据访问. 其实这篇已经在上周 完成一个大概草稿. ...

  6. uwp连接mysql数据库_在 UWP 应用中使用 SQLite 数据库

    在 UWP 应用中使用 SQLite 数据库Use a SQLite database in a UWP app 06/26/2020 本文内容 可以使用 SQLite 在用户设备上的轻量级数据库中存 ...

  7. drop sqlite 多个表_SQLite简介与安装

    SQLite简介: SQLite是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中,实现了自给自足的.无服务器的.零配置的.事务性的 SQL 数据库引擎.就像其他数据 ...

  8. SQLite学习总结(1)——SQLite简介及快速入门

    SQLite 简介 本教程帮助您了解什么是 SQLite,它与 SQL 之间的不同,为什么需要它,以及它的应用程序数据库处理方式. SQLite是一个软件库,实现了自给自足的.无服务器的.零配置的.事 ...

  9. SQLite基础知识学习

    前 言 前一段时间偶然的看到了一个名词SQLite3,大概了解到此为一种轻量型的关系型数据库.官网介绍到SQLite是一个进程内库,它实现了一个自包含的.无服务器的.零配置的事务性SQL数据库引擎(官 ...

  10. sqlite数据库中的sql语句大全-zhuan

    转载自http://hunankeda110.iteye.com/blog/1143258 2010年SQLite学习笔记之一 一. 如何获取SQLite最新版本 官方站点:http://www.sq ...

最新文章

  1. List集合add方法覆盖原来的内容解决办法
  2. ASP.NET协作应用集成到trsids身份验证服务器的开发流程
  3. Ubuntu下ICE-3.4.2的安装
  4. python瀑布图怎么做_利用Python绘制数据的瀑布图的教程
  5. Weird Game CodeForces - 299C
  6. 9月20日 DNS总结
  7. python---(4) win10 环境下访问MYSQL 数据库
  8. N个Linux耍酷命令,手把手教你如何技术撩妹!
  9. 依赖注入的三种方式_Spring IoC是如何进行依赖注入的
  10. IE打开xml文件弹出下载对话框
  11. h5页面预览pdf文件_H5怎么实现在线预览PDF
  12. 求助动态贝叶斯网络参数学习函数的使用方法
  13. 实用软件工程第二版吕云翔课后答案
  14. jquery.nicescroll参数说明
  15. (一)vmware中Linux共享文件夹设置
  16. css盒模型(标准模式和怪异模式)
  17. 极客日报:腾讯回应微信刷掌支付;iPhone 13 Pro或提供1TB版本;Git 2.33 发布
  18. 安克创新能否锚定全球家用储能市场 隆起新的增长极?
  19. Windos测试IP和端口是否能访问
  20. nmcli命令详解>>>创建热点,连接wifi,管理连接等

热门文章

  1. rehat 出现GDB debuginfo-install 问题处理
  2. cisco路由器菜单的制作
  3. pl/sql 连接远程数据库
  4. LigerUI权限系统之角色管理
  5. xcode,cocoa开发:如何使用第三方的dylib
  6. Controller类中方法返回值详解
  7. One账户多设备同步的数据库设计
  8. nginx负载均衡核心模块(upstream和proxy模块)及常用参数介绍
  9. hwclock设置日期_hwclock显示系统硬件时钟的日期和时间
  10. python time sleep 阻塞 异步_python 之 并发编程(进程池与线程池、同步异步阻塞非阻塞、线程queue)...