轉載自 編程小夢的 《windows phone 8.1開發SQlite數據庫操做詳解》sql
本文小夢將和你們分享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 ) ) ;
}
}
}
}
|
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 e )
{
SQLiteAsyncConnection conn = GetConn ( ) ;
await conn . CreateTableAsync < Note > ( ) ;
await new MessageDialog ( "建立數據庫成功!" ) . ShowAsync ( ) ;
}
|
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 ( ) ;
|
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 i = 0 ; i < notes . Count ; i ++ )
{
if ( notes [ i ] . ID == query . ID )
{
notes . RemoveAt ( i ) ;
break ;
}
}
await conn . DeleteAsync ( query as Object ) ;
listBox . ItemsSource = notes ;
await new MessageDialog ( "刪除數據成功!" ) . ShowAsync ( ) ;
|
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 i = 0 ; i < notes . Count ; i ++ )
{
if ( notes [ i ] . Name == "小夢" )
{
notes [ i ] . Content = "小夢我愛你" ;
}
}
var query = conn . Table < Note > ( ) . Where ( x = > x . Name == "小夢" ) ;
var result = await query . ToListAsync ( ) ;
foreach ( var item in result )
{
item . Content = "小夢我愛你" ;
await conn . UpdateAsync ( item ) ;
}
await new MessageDialog ( "更新數據成功!" ) . ShowAsync ( ) ;
|
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 ( ) ;
|