WI005 - Offline data sync con SQLite in Universal Windows Platform presenta Erica Barone Microsoft Technical Evangelist @_ericabarone erbarone@microsoft.com Massimo Bonanni Microsoft MVP, Intel Black Belt Intel Software Innovator @massimobonanni massimo.bonanni@tiscali.it www.wpc2015.it info@wpc2015.it - +39 02 365738.11 - #wpc15it 1
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 PCL www.wpc2015.it info@wpc2015.it - +39 02 365738.11 2
What is SQLite? 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 www.wpc2015.it info@wpc2015.it - +39 02 365738.11 - #wpc15it 3
What isn t SQLite? Not a full database application No forms No reports No saved queries www.wpc2015.it info@wpc2015.it - +39 02 365738.11 4
SQLite for Universal App Platform 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 www.wpc2015.it info@wpc2015.it - +39 02 365738.11 5
.NET APIs SQLite.NET-PCL LINQ syntax Lightweight ORM (no navigation properties) SQLitePCL SQL statements (ADO Style) Thin wrapper around the SQLite C API From Microsoft Open Technologies www.wpc2015.it info@wpc2015.it - +39 02 365738.11 6
SQLite.NET-PCL var conn = new SQLiteConnection(new Platform.WinRT.SQLitePlatformWinRT(), dbname); conn.createtable<libro>(); SQLitePCL Create a database [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; 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(); www.wpc2015.it info@wpc2015.it - +39 02 365738.11 7
SQLite.NET-PCL Query No Navigation Properties var libri = DB.Table<Libro>().Where(l => l.idautore == idautore) SQLitePCL 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)); www.wpc2015.it info@wpc2015.it - +39 02 365738.11 8
SQLite.NET-PCL CRUD Operations DB.Insert(entity, typeof(libro)) 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(); www.wpc2015.it info@wpc2015.it - +39 02 365738.11 9
SQLite.NET-PCL Transactions DB.BeginTransaction(); // do something with Database if (/* something wrong */) DB.Rollback(); else DB.Commit(); SQLitePCL 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")) statement.step(); www.wpc2015.it info@wpc2015.it - +39 02 365738.11 10
Tools www.wpc2015.it info@wpc2015.it - +39 02 365738.11 11
DEMO Biblioteca SQLite www.wpc2015.it info@wpc2015.it - +39 02 365738.11 12
Sync your data on all devices
Demo overview Offline sync SQL DB User Authentication Mobile App VS 2015 Twitter provider Authentication UWP App
Mobile App
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 backend, which in turn will modify your server database. 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.
Wrap your code into a PCL Offline sync SQL DB User Authentication Mobile App UWP App Authentication PCL Android App Twitter provider
UWP App running on PC - Android App running on Android Tablet
Q & A www.wpc2015.it info@wpc2015.it - +39 02 365738.11 - #wpc15it 21
OverNet Education info@overneteducation.it www.overneteducation.it Tel. 02 365738 Contatti OverNet Education @overnete www.facebook.com/overneteducation www.linkedin.com/company/overnet-solutions www.wpc2015.it www.wpc2015.it info@wpc2015.it - +39 02 365738.11 - #wpc15it 22