• No results found

WI005 - Offline data sync con SQLite in Universal Windows Platform

N/A
N/A
Protected

Academic year: 2021

Share "WI005 - Offline data sync con SQLite in Universal Windows Platform"

Copied!
21
0
0

Loading.... (view fulltext now)

Full text

(1)

presenta

WI005 - Offline data sync

con SQLite in Universal

Windows Platform

Erica Barone

Microsoft Technical Evangelist

@_ericabarone

[email protected]

Massimo Bonanni

Microsoft MVP, Intel Black Belt

Intel Software Innovator

@massimobonanni

(2)

Agenda

SQLite UWP

What is/isn’t SQLite

SQLite for UWP

.NET APIs

SQLite.NET-PCL vs SQLitePCL

Tools

Offline Data sync

Mobile App

SyncAsync

(3)

Open source RDBMS.

Works as library instead of service (in-process)

Single file database.

Triggers

Tables

Indices

Views

Cross Platform database (Mac, Windows, Linux).

Cross Technology Database (WPF, UWP, Win Form).

Implements most of the SQL standard (SQL92).

RIGHT and FULL OUTER JOIN

Only the RENAME TABLE and ADD COLUMN variants of the ALTER TABLE

VIEWs in SQLite are read-only.

Zero-configuration

(4)

Not a full database application

No forms

No reports

No saved queries

(5)

Contains an extension SDK and all other components needed to use SQLite for UAP

application development with Visual Studio 2015.

Visual Studio Extension (.vsix)

Install from Visual Studio (Tools – Extensions and Updates…)

Or download from SQLite.org

(6)

SQLite.NET-PCL

LINQ syntax

Lightweight ORM (no navigation properties)

.NET APIs

SQLitePCL

SQL statements (ADO Style)

Thin wrapper around the SQLite C API

(7)

var conn = new SQLiteConnection(new Platform.WinRT.SQLitePlatformWinRT(), dbName); conn.CreateTable<Libro>();

var conn = new SQLiteConnection(dbName); sql = @"CREATE TABLE IF NOT EXISTS [Libri](

[Id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, [Titolo] VARCHAR(100) NOT NULL,

[Abstract] VARCHAR(255), [ISBN] CHAR(13),

[Pagine] SMALLINT, [Copertina] IMAGE,

[IdAutore] INTEGER NOT NULL,

CONSTRAINT [Autiri_Libri] FOREIGN KEY([IdAutore]) REFERENCES Autori([Id]));";

using (var statement = conn.Prepare(sql)) { statement.Step(); }

Create a database

SQLite.NET-PCL

SQLitePCL

[Table("Libri")]

public class Libro : ILibro

{

[PrimaryKey, AutoIncrement] public long Id { get; set; } [MaxLength(100)]

public string Titolo { get; set; } [MaxLength(255)]

public string Abstract { get; set; } [MaxLength(13)]

public string ISBN { get; set; } public long Pagine { get; set; } public byte[] Copertina { get; set; } public long IdAutore { get; set; } public IAutore Autore { get; set; }

public ICollection<IRecensione> Recensioni { get; set; } }

(8)

Query

SQLite.NET-PCL

SQLitePCL

var

libri = DB.Table<Libro>().Where(l => l.IdAutore == idAutore)

using

(

var

dbconn =

new

SQLiteConnection(DatabaseName))

{

using

(

var

statement = dbconn.Prepare(

@"SELECT [Libri].[Titolo], [Libri].[Id] as IdLibro, [Libri].[Abstract],

[Libri].[ISBN], [Libri].[Pagine], [Libri].[Copertina],

[Libri].[IdAutore], [Autori].[Cognome], [Autori].[Nome]

FROM [Libri]

INNER JOIN [Autori] ON [Autori].[Id] = [Libri].[IdAutore]"

))

{

while

(SQLiteResult.ROW == statement.Step())

retList.Add(Mapper.Map<ISQLiteStatement, Libro>(statement));

}

}

No Navigation

Properties

(9)

CRUD Operations

SQLite.NET-PCL

SQLitePCL

using (var dbconn = new SQLiteConnection(DatabaseName)) {

using (var statement = dbconn.Prepare(@"INSERT INTO [Libri] ([Titolo],[Abstract],[ISBN],[Pagine],[Copertina],[IdAutore]) VALUES (@Titolo,@Abstract,@ISBN,@Pagine,@Copertina,@IdAutore)"))

{

statement.Bind("@Titolo", entity.Titolo); statement.Bind("@Abstract", entity.Abstract); statement.Bind("@ISBN", entity.ISBN);

statement.Bind("@Pagine", entity.Pagine);

statement.Bind("@Copertina", entity.Copertina); statement.Bind("@IdAutore", entity.IdAutore); result = SQLiteResult.OK == statement.Step(); }

}

(10)

Transactions

SQLite.NET-PCL

SQLitePCL

DB.BeginTransaction();

// do something with Database

if (/* something wrong */) DB.Rollback();

else DB.Commit();

using (var dbconn = new SQLiteConnection(DatabaseName)) {

using (var statement = dbconn.Prepare("BEGIN TRANSACTION")) {

statement.Step(); }

// Execute one or more statements...

using (var lbrCommand = dbconn.Prepare("INSERT INTO Libri (Titolo, Abstract, ISBN) VALUES (@Titolo, @Abstract, @ISBN)")) {

//...

}

using (var recCommand = dbconn.Prepare("INSERT INTO Recensioni (Testo, Voto, IdLibro) VALUES (@Testo, @Voto, @IdLibro)")) {

//...

}

// COMMIT to accept all changes or ROLLBACK TRANSACTION to discard pending changes

using (var statement = dbconn.Prepare("COMMIT TRANSACTION")) {

(11)
(12)

DEMO

(13)

Sync your data

on all devices

(14)

Mobile App

SQL DB

User

Authentication

Offline sync

Authentication

UWP App

VS 2015

Demo overview

(15)
(16)

SyncAsync

Every time the data change, the method SyncAsync

must be called in order to update the SQL Database

Push

sends all CUD changes since the

last push. Note that it is not possible

to send only an individual table's

changes. Push executes a series of

REST calls to your Azure Mobile App

Pull

is performed on a per-table basis and

can be customized with a query to retrieve

only a subset of the server data. The Azure

Mobile client SDKs then insert the resulting

data into the local store.

(17)
(18)

UWP App

Android App

PCL

Mobile App

SQL DB

User

Authentication

Authentication

Offline sync

(19)
(20)
(21)

Contatti

OverNet

Education

OverNet Education

[email protected]

www.overneteducation.it

Tel. 02 365738

@overnete

www.facebook.com/OverNetEducation

www.linkedin.com/company/overnet-solutions

www.wpc2015.it

www.overneteducation.it www.facebook.com/OverNetEducation www.linkedin.com/company/overnet-solutions www.wpc2015.it

References

Related documents

The quality factors for different types of radiation are: 20 for alpha, 1 for beta, gamma, and x-ray, 10 for neutrons of unknown energy (energy dependent), and 10 for high

Figure 4 shows the energy dependence of Sσ and κσ 2 for ΔN p for Au þ Au collisions for two collision central- ities (0% –5% and 70%–80%), corrected for (pð ¯pÞ)

This app, in cooperation with the eIDAS platform, lets the user perform PKI strong authentication and advanced electronic signing in any Web application either on the mobile

When a Sync occurs then prior to the Sync SME checks it has the latest state of the underlying data storage (in the event that files were added direct to the remote cloud. SME

Offline authentication is supported in both SafeNet Authentication Service Windows logon modes of operation (Dual Password and Microsoft Password Caching) with any account using

 If you are working offline while someone else changes files in a shared network folder, Windows will sync those changes with the offline files on your computer the next time

EMV chip terminals can support a variety of offline functionality (e.g., offline data authentication, offline cardholder verification and offline transaction authorization)..

The other good reasons to build a mobile app over using the mobile web are:  Your users need to access the app while offline..  The business function they are performing