[UWP小白日记-2]SQLite数据库DOME

  数据库说简单点就是增删改查,但是对新手来说也是要爆肝的。作为一个新手爆肝无数次啊,

血的教训啊现在UWP的教程又少,说多了都是泪。留下来免得以后又爆肝。还有:一定要写注释!一定要写注释!一定要写注释! 重要的事情说三遍!

  1.首先,准备工作:

  1)引用:

      

    获取途径:VS里的扩展和更新、NuGet等。

  2)数据库模型:

 1 internal class ACCOURT
 2     {
 3         public ACCOURT() { }  //空构造函数
 4        public ACCOURT(int ID,double Amount,string Descr,DateTime Time,string Mark,string Company)
 5         {//重载构造函数,用来后面把UI上的数据传入数据库
 6             this.UID = ID;
 7             this.Amount = Amount;
 8             this.Descr = Descr;
 9             this.Time = Time;
10             this.Mark = Mark;
11             this.Company = Company;
12         }
13         /// <summary>
14         /// 编号
15         /// </summary>
16         [PrimaryKey] //主键
17         [AutoIncrement]//自增
18         [NotNull]//不能为空
19         public int UID { get; set; }
20 
21         /// <summary>
22         /// 金额
23         /// </summary>
24         public double Amount { get; set; }
25 
26         /// <summary>
27         /// 备注
28         /// </summary>
29         public string Descr { get; set; }
30 
31         /// <summary>
32         /// 时间
33         /// </summary>
34         public DateTime Time { get; set; }
35 
36         /// <summary>
37         /// 标签
38         /// </summary>
39         public string Mark { get; set; }
40         
41         /// <summary>
42         /// 所属公司
43         /// </summary>
44         public string Company { get; set; }
45     }

 

  3.写帮助类:

    0)引入SQLite.net

1 using SQLite.Net;
2 using SQLite.Net.Platform.WinRT;
3 using SQLite.Net.Interop;
4 using SQLite.Net.Attributes;
5 //管它用不用先放进来

 

 

  1)数据库路径:

1  /// <summary>
2  /// 数据路径
3 /// </summary>
4  public string DbName = "SQLite.db";//名字你就随便取了
5  public string DbPath;//可以再这初始化,亦可以在后面在初始化
6 // public string DbPath=Path.Combine(ApplicationData.Current.LocalFolder.Path, DbName);

 

 

  2)创建数据库连接:

 1 #region 创建数据库链接
 2         /// <summary>
 3         /// 创建数据库连接
 4         /// </summary>
 5         /// <returns></returns>
 6         internal SQLite.Net.SQLiteConnection GetCreateConn()
 7         {
 8             DbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, DbName);
 9             var con = new SQLite.Net.SQLiteConnection(new SQLitePlatformWinRT(), DbPath);
10 
11             return con;
12 
13         }
14         #endregion

  3)创建数据库:

 1 #region 创建数据库
 2         /// <summary>
 3         /// 如果没有数据库,就创建一个数据库。
 4         /// </summary>
 5         internal void CreateDB()
 6         {/// <summary>
 7          /// 数据库文件所在路径,这里使用 LocalFolder
 8          /// </summary>
 9             DbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, DbName);
10             using (var conn = GetCreateConn())
11             {
12                 //这里什么都不写就是创建一个空数据库
13                 conn.CreateTable<ACCOURT>();//根据ACCOURT模型创建数据表
14 
15             }
16         }
17         #endregion

   特别说明:增删改这3个方法调用传入的ACCOURT addAccourt 参数,最好改为object类型,这样更具有通用性,应为一个APP不可能只有一个表

哇哈哈,写完才看到ACCOURT本来是写ACCOUNT的写错了,那就将错就错。嘎嘎。

  4)插入数据:

 1 #region
       2 internal int AddData(ACCOURT addAccourt) 3 { 4 int result = 0; 5 using (var conn = GetCreateConn()) 6 { 7 result = conn.Insert(addAccourt); 8 conn.Close(); 9 } 10 11 return result; 12 } 13 #endregion

 

  5)删除数据:

 1 #region 2         internal int DeleteData(ACCOURT AccourtUID)
 3         {
 4             int result = 0;
 5             DbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, DbName);
 6             using (var conn = GetCreateConn())
 7             {
 8                 result = conn.Delete(AccourtUID);
 9                 conn.Close();
10             }
11             return result;
12         }
13 
14         #endregion

 

  6)修改数据:

 1  #region 2         internal int UpadateData(ACCOURT updataAccourt)
 3         {
 4             int result = 0;
 5             DbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, DbName);
 6             using (var conn = GetCreateConn())
 7             {
 8                 result = conn.Update(updataAccourt);
 9                 //conn.Close();
10             }
11             return result;
12         }

 

  7)查询数据:

 1 #region 2         /// <summary>
 3         /// 模糊查询
 4         /// </summary>
 5         /// <param name="conditions">文本框输入的条件</param>
 6         /// <returns></returns>
 7         internal List<ACCOURT> CheckData(string conditions)
 8         {
 9            
10             var temSTR = "%"+conditions+"%";
11             #region 
12             using (var conn = GetCreateConn())
13             {
14                
15                 return conn.Query<ACCOURT>("select * from ACCOURT where Company like ? or Descr like ? or UID like ? or Mark like ? or Time like ? or Amount like ?;", temSTR, temSTR, temSTR, temSTR, temSTR, temSTR);
16               
17                
18 
19             }
20             #endregion
21         }
22 
23         #endregion

  查询只个方法要说明下:这个他的原型:

1 Query<ACCOURT>(string sql,params objcet [] args)

Sql:这就不用说明了就是SQL语句。

params:不确定个数的参数列表。

  

1 Query<ACCOURT>("select * from ACCOURT where Company like ? or Descr like ? or UID like ? or Mark like ? or Time like ? or Amount like ?;", temSTR, temSTR, temSTR, temSTR, temSTR, temSTR)
2 //多字段模糊查询,SQL中有多少个"?"就在后面的params中输入多少个参数

 

  8)读取数据:

 1 #region 2         internal ObservableCollection<ACCOURT> ReadData(ObservableCollection<ACCOURT> accourt)
 3         {
 4             accourt.Clear();
 5             DbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, DbName);
 6             CreateDB();
 7             using (var conn = GetCreateConn())
 8             {
 9                 var dbAccourt = conn.Table<ACCOURT>();
10                 foreach (var item in dbAccourt)
11                 {
12                     accourt.Add(item);
13                 }
14                 //conn.Close();
15             }
16             return accourt;
17         }
18         #endregion

总结

可以看出:增、删、改 这3个方法是差不多的

查询要复杂一点,我想其他任何数据库应该都差不多,我自己目前还没有接触过其他的数据库。

哦,还有一点忘记了,在VS用代码分析的时候会提示:conn.Close(); 多次释放,最好还是删除它。                           

 

欢迎大家留言交流

最后是一个dome的代码:

下载

 

posted @ 2016-06-26 21:47  Enious  阅读(893)  评论(0编辑  收藏  举报