在Win8 Mertro 中使用SQLite

首先先介绍下sqlitemetro下应用简介:c++

sqlite的网是彻底符合WinRTMetro风格的应用程序,并经过微软的商店验证。git

数据库文件将始终被建立的路径返回:github

Windows.Storage.ApplicationData.Current.LocalFolder.Pathsql

您将须要一个副本的sqlite3.dll已编译对SQLite.orgWinRT分支。虽然这不是在主流支数据库

持,它被预期。你能够找到更多信息并下载适当的编译sqlite3.dllwindows

https://github.com/mbrit/sqlite-metrostyle ]app

咱们提供了一个WinRT / Windows 8sqlite3.dllasync

 

好了:如今开始先介绍下怎么安装sqlite有图有真相:工具

首先1.建立一个metro项目spa

2.在工具,选择扩展与更新中,选择联机(online),在搜索框内输入sqlite

3.将会发现一个叫作sqlite for window runtime点击安装

4.

5.在引用中,选择windows,扩展,把Mircosoft visual c++ runtime package以及sqlite for windows runtime两者勾选上

 

6.点选解决方案,选择属性,配置属性,将平台选择为对应的平台,暂时不支持any cpu

 

7.点击工程,选择管理nuget包(若是没有须要另行安装),在联机中搜索sqlite-net,点击安装

 

8安装成功后会生成两个文件:SQLite.csSQLiteAsync到此完成安装部署

下面就是举一个简单的举例进行了解sqlite的使用;

首先:声明一个person类也就是表主键自动增加

 class Person

    {

         [SQLite.AutoIncrement, SQLite.PrimaryKey]

          public int ID { get; set; }

          public string FirstName { get; set; }

         public string LastName { get; set; } 

 

}

2.写一个方法用于建立数据库db1.sqlite和表person

          private async void Create()

        {

            //数据文件保存的位置

         Var  dbPath=Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");

            //打开建立数据库和表

          using (var db = new SQLite.SQLiteConnection(dbPath))

          {

              //建立表

          db.CreateTable<Person>();

        

          }

           

        }

3.简单的操做sqlite数据库(增,删,改,查询)

 //sqlite语句简单示例

            //链接数据库

           var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");

           var db = new SQLite.SQLiteConnection(dbPath);

          //插入操做。首先声明一个集合

           ObservableCollection<Person> Collection = new ObservableCollection<Person>();

          //单条插入语句

           db.Insert(new Person() { FirstName = "liufei", LastName = "Sky" });

           Collection.Add(new Person() { FirstName = "liufei1", LastName = "Sky" });

           Collection.Add(new Person() { FirstName = "liufei3", LastName = "Sky" });

            //多条插入集合

           db.InsertAll(Collection);

          

            //更新语句

           SQLiteCommand cmd = db.CreateCommand("update person set FirstName='lisa'where FirstName='liufei'");

           cmd.ExecuteNonQuery();

 

          //单行删除操做

           db.Delete<Person>(new Person() { ID = 1 });

            //多行删除

           db.DeleteAll<Person>();

            //查询全部数据绑定到UI

           List<object> list = db.Query(new TableMapping(typeof(Person)), "select *  from  Person");

           lt.ItemsSource = list;