【转载】wp 8.1开发SQlite数据库操作详解

 

转载自 编程小梦的 《windows phone 8.1开发SQlite数据库操作详解》

本文小梦将和大家分享WP8.1中SQlite数据库的基本操作:(最后有整个示例的源码)(希望能通过本站广告支持小梦,谢谢!)

  • 建立数据库
  • 增加数据
  • 删除数据
  • 更改数据
  • 查询数据

(注:为了让每个操作都能及时显示在UI上,所以进行了数据绑定.数据绑定会在后面文章专门讲解,先给出数据类Note,代表一个笔记.含有Name 和content  属性.其代码如下:如果不清楚,我会在之后讲解):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
namespace SQlite
{
public class Note INotifyPropertyChanged
{
private int id ;
AutoIncrement PrimaryKey ]
public int ID
{
get return id }
set
{
if value != id )
{
id value ;
RaisePropertyChanged "ID" ;
 
}
}
}
private string name ;
MaxLength 30 ]
public string Name
{
get return name }
set
{
if value != name )
{
name value ;
RaisePropertyChanged "Name" ;
 
}
}
}
 
private string content ;
MaxLength 300 ]
public string Content
{
get return content }
set
{
if value != content )
{
content value ;
RaisePropertyChanged "Content" ;
 
}
}
}
 
public event PropertyChangedEventHandler PropertyChanged ;
protected void RaisePropertyChanged string propertyName )
{
if PropertyChanged != null )
{
PropertyChanged this new PropertyChangedEventArgs propertyName ;
}
}
}
}

 

windows phone 8.1开发SQlite-建立数据库

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private SQLiteAsyncConnection GetConn )
{
 
return new SQLiteAsyncConnection ApplicationData Current LocalFolder Path "\\note.db" ;
 
}
 
private async void createButton_Click object sender RoutedEventArgs )
{
 
SQLiteAsyncConnection conn GetConn ;
 
await conn CreateTableAsync Note ;
 
await new MessageDialog "创建数据库成功!" ShowAsync ;
}

 

windows phone 8.1开发SQlite-增加数据

 

1
2
3
4
5
6
7
8
9
10
11
12
SQLiteAsyncConnection conn GetConn ;
 
await conn InsertAsync new Note Name "小梦" Content "小梦我想你" ;
await conn InsertAsync new Note Name "小梦" Content "小梦我爱你" ;
await conn InsertAsync new Note Name "小梦" Content "小梦我喜欢你" ;
await conn InsertAsync new Note Name "小梦" Content "小梦我恨你" ;
await conn InsertAsync new Note Name "小梦" Content "小梦我打你" ;
 
List Note notelist await conn Table Note ToListAsync ;
notes new ObservableCollection Note notelist ;
listBox ItemsSource notes ;
await new MessageDialog "增加数据成功!" ShowAsync ;

 

windows phone 8.1开发SQlite-删除数据

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
SQLiteAsyncConnection conn GetConn ;
var query await conn Table Note FirstAsync ;
for int notes Count ++ )
{
if notes ID == query ID )
{
notes RemoveAt ;
break ;
}
}
 
await conn DeleteAsync query as Object ;
listBox ItemsSource notes ;
await new MessageDialog "删除数据成功!" ShowAsync ;

 

windows phone 8.1开发SQlite-修改数据

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
SQLiteAsyncConnection conn GetConn ;
for int notes Count ++ )
{
if notes Name == "小梦" )
{
notes Content "小梦我爱你" ;
 
}
}
var query conn Table Note Where Name == "小梦" ;
 
var result await query ToListAsync ;
 
foreach var item in result )
{
 
item Content "小梦我爱你" ;
 
await conn UpdateAsync item ;
}
 
await new MessageDialog "更新数据成功!" ShowAsync ;

 

windows phone 8.1开发SQlite-查询数据

 

1
2
3
4
5
6
7
8
SQLiteAsyncConnection conn GetConn ;
 
var query conn Table Note ;
 
var result await query ToListAsync ;
notes new ObservableCollection Note result ;
listBox ItemsSource notes ;
await new MessageDialog "查询数据成功!" ShowAsync ;

 

 

 
posted @ 2016-01-30 16:21  鲁氏新之助  阅读(172)  评论(0编辑  收藏  举报