windows phone 8.1开发SQlite数据库操作详解

原文出自:http://www.bcmeng.com/windows-phone-sqlite1/

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

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

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

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()]
public string Name
{
get { return name; }
set
{
if (value != name)
{
name = value;
RaisePropertyChanged("Name"); }
}
} private string content;
[MaxLength()]
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-建立数据库

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();
}

windows phone 8.1开发SQlite-增加数据

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-删除数据

SQLiteAsyncConnection conn = GetConn();
var query = await conn.Table<Note>().FirstAsync();
for (int i = ; 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();

windows phone 8.1开发SQlite-修改数据

SQLiteAsyncConnection conn = GetConn();
for (int i = ; 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();

windows phone 8.1开发SQlite-查询数据

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();

windows phone 8.1开发SQlite数据库操作详解源码下载:

点我下载!

上一篇:《工作笔记:移动web页面前端开发总结》


下一篇:K8S之rbac