剛剛測試了wp8裏的sqlite功能,有點小波折,不過一切都還順利。現在寫出來和大家分享一下吧!
可以說整個過程分爲4步(先新建一個項目再開始以下步驟):
1)在Vs2012中打開工具(tools)裏的擴展和更新(extensions and updates)搜索 sqlite for windows phone然後下載安裝2)
2)打開工具 ---〉庫程序包管理器--->管理器控制檯
然後輸入 Install-package sqlite-net 回車
等待一會就會提示安裝完成
此時應該會有兩個新文件:SQLite.cs SQLiteAsync.cs添加到項目裏
3)到 https://github.com/peterhuene/sqlite-net-wp8 頁面中 點擊右邊下方的download 下載此項目 解壓後 在解決方案資源管理器
(一般在右上的那個框,除非你調了)裏的解決方案上右鍵,添加已有項目,把剛下載的項目添加進來,然後添加引用sqlite(引用---〉右鍵--->添加引用--->解決方案--->sqlite)
4)右鍵你的項目,然後點擊屬性,點擊右邊的生成,在條件編譯符號里加上;USE_WP8_NATIVE_SQLITE 使其變爲
此時你的sqlite便可以運行了。
關於測試代碼:
namespace SQLiteTest { public partial class MainPage : PhoneApplicationPage { private SQLiteConnection mySqlite = null; // 構造函數 public MainPage() { InitializeComponent(); } private void InitializeDB() { mySqlite = new SQLiteConnection("SQLiteTestDB"); mySqlite.CreateTable<People>(); } protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { if (mySqlite != null) { mySqlite.Close(); } base.OnNavigatingFrom(e); } protected override void OnNavigatedTo(NavigationEventArgs e) { InitializeDB(); RefreshListBox(); base.OnNavigatedTo(e); } private void btnAdd_Click(object sender, RoutedEventArgs e) { string name = txbName.Text; int age = Int32.Parse(txbAge.Text); People people = new People { Id = Guid.NewGuid().ToString(), Name = name, Age = age }; mySqlite.BeginTransaction(); mySqlite.Insert(people); mySqlite.Commit(); RefreshListBox(); } private void RefreshListBox() { List<People> peopleList = mySqlite.Table<People>().ToList<People>(); lbxDB.ItemsSource = peopleList; } } }
和People類
namespace SQLiteTest { public sealed class People { [PrimaryKey] public string Id { set; get; } public string Name { set; get; } public int Age { set; get; } public People() { } public People(string name, int age) { Name = name; Age = age; } } }
就給大家了。如果有什麼問題歡迎討論!