WinRT Apps
+ Silverlight
30 April 2014
Building Apps for Windows Phone 8.1 Jump Start
Complex Schema
ItemReferenceData
PK ItemId
ItemName
ItemDescription
FK1 CategoryId
Categories
PK CategoryId
CategoryName
Lists
PK ListId
ListName
ListItems
PK ListItemId
ListItemName
FK1 ListId
Quantity
Category
Description
FK2 StoreId
Stores
PK StoreId
StoreName
StoreLocationLat
StoreLocationLong
StoreAddressLine1
StoreAddressLine2
StoreAddressCity
StoreAddressState
StoreAddressCountry
StoryAddressZip
Favorites
PK FavoriteItemId
FavoriteItemName
FavoriteItemCategory
FavoriteItemQuantity
FavoriteItemDescription
FK1 FavoriteItemListId
FavoriteItemPhoto
History
PK HistoryItemId
HistoryItemName
HistoryItemCategory
HistoryItemQuantity
HistoryItemDescriptioin
HistoryItemDateAdded
FK1 HistoryItemListId
HistoryItemPhoto
Reference Data
Words
PK WordId
Word
Pronunciation
Definition
AlternateSpellings
Origin
Favorites
PK FavoriteId
FK1 WordId
History
PK HistoryItemId
FK1 WordId
AddedDate
using (var db = new SQLitePCL.Database("demo.db"))
{
db.Open();
using (var stmt = db.PrepareStatement
("SELECT name, age FROM people"))
{
while (stmt.Step())
{
var name = stmt.GetTextAt(0);
var age = stmt.GetIntAt(1);
}
}
}
var db =
new SQLite.SQLiteAsyncConnection(App.DBPath);
var _customer = await
(from c in db.Table<Customer>()
where c.Id == customerId
select c).FirstOrDefaultAsync();
if (customer != null)
{
var Id = _customer.Id;
var Name = _customer.Name;
}
http://bit.ly/Zxg2Ox
http://bit.ly/MuzL1e
http://bit.ly/130PpGa
private void LoadDatabase()
{
// Get a reference to the SQLite database
conn = new SQLiteConnection("sqlitepcldemo.db");
string sql = @"CREATE TABLE IF NOT EXISTS
Customer (Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name VARCHAR( 140 ),
City VARCHAR( 140 ),
Contact VARCHAR( 140 )
);";
using (var statement = conn.Prepare(sql))
{
statement.Step();
}
}
// SqlConnection was opened in App.xaml.cs and exposed through property conn
var db = App.conn;
try
{
using (var custstmt = db.Prepare("INSERT INTO Customer (Name, City, Contact) VALUES (?, ?, ?)"))
{
custstmt.Bind(1, customerName);
custstmt.Bind(2, customerCity);
custstmt.Bind(3, customerContact);
custstmt.Step();
}
}
catch (Exception ex)
{
// TODO: Handle error
}
public Customer GetCustomer(int customerId)
{
Customer customer = null;
using (var statement = dbconn.Prepare("SELECT Id, Name, City, Contact FROM Customer WHERE Id = ?"))
{
statement.Bind(1, customerId);
if (SQLiteResult.DONE == statement.Step())
{
customer = new Customer()
{
Id = (long)statement[0],
Name = (string)statement[1],
City = (string)statement[2],
Contact = (string)statement[3]
};
}
}
return customer;
}
// See if the customer already exists
var existingCustomer = GetCustomer(customer.Id);
if (existingCustomer != null)
{
using (var custstmt = dbconn.Prepare("UPDATE Customer SET Name = ?, City = ?, Contact = ? WHERE Id=?"))
{
// NOTE when using anonymous parameters the first has an index of 1, not 0.
custstmt.Bind(1, customer.Name);
custstmt.Bind(2, customer.City);
custstmt.Bind(3, customer.Contact);
custstmt.Bind(4, customer.Id);
custstmt.Step();
}
}
public void DeleteCustomer(int customerId)
{
using (var statement = dbconn.Prepare("DELETE FROM Customer WHERE Id = ?"))
{
statement.Bind(1, customerId);
statement.Step();
}
}
using (var statement = dbconn.Prepare("BEGIN TRANSACTION"))
{
statement.Step();
}
// Execute one or more statements…
using (var custstmt = dbconn.Prepare("INSERT INTO Customer (Name, City, Contact) VALUES (?, ?, ?)"))
{
...
}
using (var projstmt = dbconn.Prepare("INSERT INTO Project (Name, Title, DueDate, CustomerId) VALUES (?, ?, ?, ?)"))
{
...
}
// COMMIT to accept all changes or ROLLBACK TRANSACTION to discard pending changes
using (var statement = dbconn.Prepare(“COMMIT TRANSACTION"))
{
statement.Step();
}
Customer
PK Id
Name
City
Contact
Project
PK Id
Name
Description
DueDate
FK1 CustomerId
Project
PK Id
Name
Description
DueDate
FK1 CustomerId
Project
PK Id
Name
Description
DueDate
FK1 CustomerId
Customer
PK Id
Name
City
Contact
Project
PK Id
Name
Description
DueDate
FK1 CustomerId
Project
PK Id
Name
Description
DueDate
FK1 CustomerId
CREATE TABLE IF NOT EXISTS Project
(Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
CustomerId INTEGER,
Name VARCHAR( 140 ),
Description VARCHAR( 140 ),
DueDate DATETIME,
FOREIGN KEY(CustomerId) REFERENCES Customer(Id)
);
// Turn on Foreign Key constraints
sql = @"PRAGMA foreign_keys = ON";
using (var statement = dbconn.Prepare(sql))
{
statement.Step();
}
Type Description SQLiteWinRT
PRIMARY KEY Defines the column(s) of the primary key
- 1 per table max
Yes
In Column or Table definition in a
CREATE TABLE SQL statement
UNIQUE Column constraint enforces unique values
in that column
Yes
In Column definition
NOT NULL Column constraint prevents null values Yes
In Column definition
CHECK Column or Table constraint: constraint
expression is evaluated on every insert or
update, and if ‘0’ returned, constraint fails
Yes
In Column or Table definition
http://sqlite.org/lang_createtable.html
// Create index on Foreign Key column
sql = @"CREATE INDEX IF NOT EXISTS
fk_customer_project_idx
ON project (customerId) ASC";
using (var statement = dbconn.Prepare(sql))
{
statement.Step();
}
http://www.sqlite.org/cvstrac/wiki?p=ManagementTools
http://sqlitestudio.pl/
http://sqliteadmin.orbmu2k.de/
http://www.sqliteexpert.com/
©2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in the
U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft
must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after
the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

19 programming sq lite on windows phone 8.1

  • 1.
    WinRT Apps + Silverlight 30April 2014 Building Apps for Windows Phone 8.1 Jump Start
  • 2.
    Complex Schema ItemReferenceData PK ItemId ItemName ItemDescription FK1CategoryId Categories PK CategoryId CategoryName Lists PK ListId ListName ListItems PK ListItemId ListItemName FK1 ListId Quantity Category Description FK2 StoreId Stores PK StoreId StoreName StoreLocationLat StoreLocationLong StoreAddressLine1 StoreAddressLine2 StoreAddressCity StoreAddressState StoreAddressCountry StoryAddressZip Favorites PK FavoriteItemId FavoriteItemName FavoriteItemCategory FavoriteItemQuantity FavoriteItemDescription FK1 FavoriteItemListId FavoriteItemPhoto History PK HistoryItemId HistoryItemName HistoryItemCategory HistoryItemQuantity HistoryItemDescriptioin HistoryItemDateAdded FK1 HistoryItemListId HistoryItemPhoto
  • 3.
    Reference Data Words PK WordId Word Pronunciation Definition AlternateSpellings Origin Favorites PKFavoriteId FK1 WordId History PK HistoryItemId FK1 WordId AddedDate
  • 7.
    using (var db= new SQLitePCL.Database("demo.db")) { db.Open(); using (var stmt = db.PrepareStatement ("SELECT name, age FROM people")) { while (stmt.Step()) { var name = stmt.GetTextAt(0); var age = stmt.GetIntAt(1); } } } var db = new SQLite.SQLiteAsyncConnection(App.DBPath); var _customer = await (from c in db.Table<Customer>() where c.Id == customerId select c).FirstOrDefaultAsync(); if (customer != null) { var Id = _customer.Id; var Name = _customer.Name; }
  • 8.
  • 13.
    private void LoadDatabase() { //Get a reference to the SQLite database conn = new SQLiteConnection("sqlitepcldemo.db"); string sql = @"CREATE TABLE IF NOT EXISTS Customer (Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Name VARCHAR( 140 ), City VARCHAR( 140 ), Contact VARCHAR( 140 ) );"; using (var statement = conn.Prepare(sql)) { statement.Step(); } }
  • 14.
    // SqlConnection wasopened in App.xaml.cs and exposed through property conn var db = App.conn; try { using (var custstmt = db.Prepare("INSERT INTO Customer (Name, City, Contact) VALUES (?, ?, ?)")) { custstmt.Bind(1, customerName); custstmt.Bind(2, customerCity); custstmt.Bind(3, customerContact); custstmt.Step(); } } catch (Exception ex) { // TODO: Handle error }
  • 15.
    public Customer GetCustomer(intcustomerId) { Customer customer = null; using (var statement = dbconn.Prepare("SELECT Id, Name, City, Contact FROM Customer WHERE Id = ?")) { statement.Bind(1, customerId); if (SQLiteResult.DONE == statement.Step()) { customer = new Customer() { Id = (long)statement[0], Name = (string)statement[1], City = (string)statement[2], Contact = (string)statement[3] }; } } return customer; }
  • 16.
    // See ifthe customer already exists var existingCustomer = GetCustomer(customer.Id); if (existingCustomer != null) { using (var custstmt = dbconn.Prepare("UPDATE Customer SET Name = ?, City = ?, Contact = ? WHERE Id=?")) { // NOTE when using anonymous parameters the first has an index of 1, not 0. custstmt.Bind(1, customer.Name); custstmt.Bind(2, customer.City); custstmt.Bind(3, customer.Contact); custstmt.Bind(4, customer.Id); custstmt.Step(); } }
  • 17.
    public void DeleteCustomer(intcustomerId) { using (var statement = dbconn.Prepare("DELETE FROM Customer WHERE Id = ?")) { statement.Bind(1, customerId); statement.Step(); } }
  • 21.
    using (var statement= dbconn.Prepare("BEGIN TRANSACTION")) { statement.Step(); } // Execute one or more statements… using (var custstmt = dbconn.Prepare("INSERT INTO Customer (Name, City, Contact) VALUES (?, ?, ?)")) { ... } using (var projstmt = dbconn.Prepare("INSERT INTO Project (Name, Title, DueDate, CustomerId) VALUES (?, ?, ?, ?)")) { ... } // COMMIT to accept all changes or ROLLBACK TRANSACTION to discard pending changes using (var statement = dbconn.Prepare(“COMMIT TRANSACTION")) { statement.Step(); }
  • 22.
    Customer PK Id Name City Contact Project PK Id Name Description DueDate FK1CustomerId Project PK Id Name Description DueDate FK1 CustomerId Project PK Id Name Description DueDate FK1 CustomerId Customer PK Id Name City Contact Project PK Id Name Description DueDate FK1 CustomerId Project PK Id Name Description DueDate FK1 CustomerId
  • 23.
    CREATE TABLE IFNOT EXISTS Project (Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, CustomerId INTEGER, Name VARCHAR( 140 ), Description VARCHAR( 140 ), DueDate DATETIME, FOREIGN KEY(CustomerId) REFERENCES Customer(Id) );
  • 24.
    // Turn onForeign Key constraints sql = @"PRAGMA foreign_keys = ON"; using (var statement = dbconn.Prepare(sql)) { statement.Step(); }
  • 25.
    Type Description SQLiteWinRT PRIMARYKEY Defines the column(s) of the primary key - 1 per table max Yes In Column or Table definition in a CREATE TABLE SQL statement UNIQUE Column constraint enforces unique values in that column Yes In Column definition NOT NULL Column constraint prevents null values Yes In Column definition CHECK Column or Table constraint: constraint expression is evaluated on every insert or update, and if ‘0’ returned, constraint fails Yes In Column or Table definition http://sqlite.org/lang_createtable.html
  • 27.
    // Create indexon Foreign Key column sql = @"CREATE INDEX IF NOT EXISTS fk_customer_project_idx ON project (customerId) ASC"; using (var statement = dbconn.Prepare(sql)) { statement.Step(); }
  • 31.
  • 35.
    ©2014 Microsoft Corporation.All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.