SQLite in Windows Phone 8
SQLite in Windows Phone 8
KASIDIT WIJITSOPON
M EG A G E N I U S CO. , LT D.
Requirement
- SQLite for Windows Phone SDK
- Git for Windows
- C++ Wrapper (sqlite-net-wp8)
- C# class (sqlite-net)
Click on TOOLS
Extensions and Updates
Click on Online
Search for
SQLite for Windows Phone
Add Reference
SQLite for Windows Phone SDK
1.
2.
3.
Sample use
1. Create a class which defines the table.
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int Id { get; set; }
public string FirstName { get; set; }
Sample use
2. Create a database in the constructor (App.xaml.cs). If the database
string dbPath =
Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,
"db.sqlite");
if (!FileExists("db.sqlite").Result)
{
using (var db = new SQLiteConnection(dbPath))
{ db.CreateTable<Person>(); }
}
Sample use
private async Task<bool> FileExists (string fileName)
{
var result = false;
try
{
var store = await
Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
result =true;
}
catch { }
return result;
}
Sample use
3. Create button
- Insert (btnInsert)
- Select (btnSelect)
Sample use
4. Create method btnInsert_Click
private void btnInsert_Click (sender, RoutedEventArgs e)
{
});
}
}
Sample use
5. Create async method btnSelect_Click
private async void btnSelect_Click(object sender, RoutedEventArgs e)
{
SQLiteAsyncConnection conn = new SQLiteAsyncConnection(dbPath);
var persons = await conn.QueryAsync<Person>("SELECT * FROM Person");
foreach (var person in persons)
Sample use
5. Run Project
Click on Insert button.
Click on Select button.
#Thank You