The new Portable Class Library for SQLite z

Microsoft Open
Technologies
has recently released a Portable Class Library for SQLite. Thanks to it, we can
use SQLite in the same way in all the supported platforms. Let’s see how to do
that.

As prerequisite, we need to install the SQLite Extension SDK that corresponds
to our platform. For example, for Windows 8.1, it is the SQLite for Windows Runtime (Windows 8.1) v3.8.2 extension
SDK
. The other versions are available on the SQLite official download page. Once downloaded, it’s necessary to change the
extension from ZIP to VSIX, then double click it to install the SDK. Now we can
add the extension to the project using the Add reference
command:

SQLite for Windows Runtime

Because it is a native library, the “Any CPU” architecture is not supported,
so we need to choose a specific target platform: Visual studio will reference
the appropriate extension SDK version the the project compiles.

Finally, let’s use NuGet
to install the Portable Class Library for SQLite:

SQLitePCL on NuGet

Now everything is ready to start using the library. Suppose for example we
want to create a database named Storage.db with a
People table:

?





1

2

3

4

5

6

7

8

9

10

using
(var
connection = new
SQLiteConnection("Storage.db"))

{

    using
(var
statement = connection.Prepare(@"CREATE TABLE IF NOT EXISTS People (

                                                ID INTEGER NOT NULL PRIMARY KEY,

                                                FirstName NVARCHAR(50),

                                                LastName NVARCHUAR(50));"))

    {

        statement.Step();

    }

}

First of all, we create an SQLiteConnection object that
points to the specified file. If it isn’t rooted, the library assumes that it is
located in the ApplicationData.Current.LocalFolder folder (the same
assumption applies also for Windows Phone 8).

At this moment, SQLite PCL supports only direct SQL commands (no LINQ
provider). At line 3, we use the connection.Prepare method to
define the DDL query we want to execute. Then, on line 8, with
statement.Step, we send the query to the database engine, that
immediately executes it.

The following example shows how to insert data:

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

using
(var
statement = connection.Prepare(@"INSERT INTO People (FirstName, LastName)

                                            VALUES(@firstName, @lastName);"))

{

    statement.Bind("@firstName", "Donald");

    statement.Bind("@lastName", "Duck");

    // Inserts data.

    statement.Step();

    // Resets the statement, to that it can be used again (with different parameters).

    statement.Reset();

    statement.ClearBindings();

    statement.Bind("@firstName", "Mickey");

    statement.Bind("@lastName", "Mouse");

    // Inserts data.

    statement.Step();

}

Again, the Prepare method is used to define the SQL command. In this case, it
is an INSERT in which we have defined two parameters, @firstName and
@lastName. At line 4-5, we bind them to their actual values, using the
Bind method. The Step command (line 8) finalizes the
operation.

Then, because we want to reuse the same statement to insert another record,
we need to call Reset (line 11), that resets the prepared
statement back to its initial state, ready to be re-executed, and
ClearBindings (line 12), to remove the bindings that have been
defined before.

Finally, it’s the moment to retrieve the saved data:

?





1

2

3

4

5

6

7

8

9

using
(var
statement = connection.Prepare(@"SELECT * FROM People ORDER BY FirstName;"))

{

    while
(statement.Step() == SQLiteResult.ROW)

    {

        var
id = (long)statement[0];

        var
firstName = (string)statement[1];

        var
lastName = (string)statement[2];

    }

}

To read the records returned by the query, we need to iterate through the
rows, in a way that resembles the SqlDataReader.Read method.

In order to retrieve the actual values, we need to use the indexer operator
on the statement object, specifying the column number. As this
method gets a result of Object type, we need to cast it to the real type of the
column. If we want to avoid this syntax, and instead prefer to use generics, we
can define a simple extension method:

?





1

2

3

4

5

6

7

public
static class SQLitePCLExtensions

{

    public
static T GetValue<T>(this
ISQLiteStatement statement, int
index)

    {

        return
(T)statement[index];

    }

}

And so in the previous loop we can write:

?





1

2

3

var id = statement.GetValue<long>(0);

var firstName = statement.GetValue<string>(1);

var lastName = statement.GetValue<string>(2);

As we have seen, this library is very straightforward. Its usage mimics the
native C++ library (the Prepare, Step and Reset methods, for example), with a
great advantage: we can code against one single API, regardless of whether we’re
developing Windows Store, Windows Phone or .NET 4.5 projects.

More information about the Portable Class Library for SQLite are available on
CodePlex.

时间: 2025-01-13 07:00:07

The new Portable Class Library for SQLite z的相关文章

使用 Portable Class Library(可移植类库)开发 Universal Windows App

今天在这里跟大家聊聊关于 Windows Universal 应用夸平台的问题,首先Universal Windows App的定义相信大家已经有所了解了(如果你是一个刚刚接触 Universal APP 的开发这个请先阅读一下我之前的文章 Windows Phone 8.1 开发技术概览 [Universal APP]), 相信大家在这里最苦恼的事情莫过于在不同开发架构下分享代码了,今天我在这里给大家推荐一个解决方案使用可移植类库(Portable Class Library)在不同的Wind

C++ Development Library

C/C++ 开发库 | C/C++ Development Library 这里收集一些著名的 C/C++ 开发库.SDK.类库.可复用类与结构代码 等信息,列举它们的介绍.参考和网站链接,为各位 C/C++ 程序员和爱好者提供检索和查阅类库的方便 下面收集的 C/C++ 类库介绍整理来源于文章:C++ 资源之不完全导引(作者:曾毅.陶文),这篇文章曾发表于 2004 年 5 月<CSDN 开发高手> 上文中介绍的类库有些已经多年未见发布和网站内容的更新了,特别是一些开源的项目.我检查了作者提

【Android】13.0 第13章 创建和访问SQLite数据库&mdash;本章示例主界面

分类:C#.Android.VS2015: 创建日期:2016-02-26 一.简介 Android 内置了三种数据存取方式:SQLite数据库.文件.SharedPreferences. 这一章我们主要学习如何使用SQLite数据库存取数据. 1.SQLite是个什么档次的数据库 SQLite是一种免费的.开源的数据库,由于它独特的设计(把各种数据类型都转换为它自己内部处理的5种类型)导致其占用内存极少,因此很多项目都喜欢使用它. Android集成了SQLite并内置了专门对SQLite操作

How to Make Portable Class Libraries Work for You

A Portable Class Library is a .NET library that can be used (in binary form, without recompiling) on multiple .NET platforms.  When you create a Portable Class Library in Visual Studio, you can choose which platforms to target.  Portable libraries su

Inside Portable Class Libraries

Portable Class Libraries were introduced with Visual Studio 2010 SP1 to aid writing libraries that could be used on many different platforms – the full .NET 4/4.5 framework, Windows Phone, Silverlight, Xbox, and Windows Store apps. You simply select

7.6 yum更换国内源 7.7 yum下载rpm包 7.8/7.9 源码包安装

7.6 yum更换国内源 7.7 yum下载rpm包 7.8/7.9 源码包安装 扩展 1. 配置yum源优先级 http://ask.apelearn.com/question/7168 2. 把源码包打包成rpm包 http://www.linuxidc.com/Linux/2012-09/70096.htm # 7.6  yum更换国内源 ![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170812/154757717.png?imagesl

C++开源库集合

| Main | Site Index | Download | mimetic A free/GPL C++ MIME Library mimetic is a free/GPL Email library (MIME) written in C++ designed to be easy to use and integrate but yet fast and efficient. It is based on the C++ standard library and heavily us

Linux中如何安装Apache服务器

由于学习的需要,所有手动安装了一下Apache源码包,安装过程中的问题千奇百怪,但是如果弄清楚了问题出在哪里,那么也不是太难.如果有学习者出现安装中的问题,可仔细阅读该教程. 首先下载httpd软件包(下载地址http://httpd.apache.org/download.cgi#apache24). 由于本人是在虚拟机中安装的CentOs7.0,所以我们还需要下载一个软件用来将下载在Windows中的包文件放置在Linux中.(下载地址:http://winscp.net/eng/docs/

C++ 著名程序库 概览

      本文转载自: http://ace.acejoy.com/thread-3777-1-1.html   1.C++各大有名库的介绍--C++标准库 2.C++各大有名库的介绍--准标准库Boost 3.C++各大有名库的介绍--GUI 4.C++各大有名库的介绍--网络通信 5.C++各大有名库的介绍--XML 6.C++各大有名库的介绍--科学计算 7.C++各大有名库的介绍--游戏开发 8.C++各大有名库的介绍--线程 9.C++各大有名库的介绍--序列化 10.C++各大有名