session 8 connect your universal application with database .. builders & developers workshop...

34
Builders & Developers Workshop SESSION 8 PREPARED & PRESENTED BY : MOATASIM MAGDY

Upload: moatasim-magdy

Post on 06-Aug-2015

221 views

Category:

Education


0 download

TRANSCRIPT

Builders & DevelopersWorkshopSESSION 8

PREPARED & PRESENTED BY : MOATASIM MAGDY

Our topic today Database

Why database ?

Queries Create Select Insert Update Delete Drop

Create Table CREATE TABLE table_name ( Column_name1 data_type(Size), Column_name2 data_type(Size), Column_name3 data_type(Size), ……. );

Insert Query INSERT INTO table_name VALUES (value1, value2 , value3 …);

OR

INSERT INTO table_name (column1, column2, column3 ….) VALUES (value1, value2 , value3 …);

Select query SELECT column_name, column_name2 FROM table_name WHERE column_name operator value;

OR SELECT * FROM table_name WHERE column_name operator value;

Operators = Equal <> Not equal > Greater than < Less than >= Greater than <= Less than LIKE Search for pattern

Update query UPDATE table_name

SET column1 = VALUE, column2 = VALUE

WHERE some_column = some_value;

Delete Query DELETE FROM table_name WHERE some_column = some_value;

Drop Table DROP TABLE table_name

How to connect Your Universal application with sqlite ?

Step 1 First press tools then open extensions and updates, make sure these 2 extensions are available.

Step 2 Then from solution explorer right click on references from windows 8.1 part then select add reference.

Step 3 After you open reference manager in windows phone select this.

Step 4 Then from solution explorer right click on references from windows 8.1 part then select add reference.

Step 5 After you open reference manager in windows phone select this.

Step 6 You will notice these warnings in windows and windows phone

Step 7 To solve this warnings open configuration manager from the Debug in the top bar of visual studio.

Step 8 Right click on the project from solution and explorer then open “manage nugget packages” and search for sqlite-net then install.

Step 9To make sure every thing is going right you`ll notice appearance of this 2 classes in windows and windows phone part.

Connect your database with C# Create Database. Check existence of database. Create Table , Create using database connection. Add record(s) to your table Select record from your table Remove record from your table Update record from your table Drop table

Database Open create if not existence

Check if not exist

Table We start defining the class that represent the table row -> Then add the table to the database

Add a record to a table Add one record. Add more than one record

Select record from your table SQLiteAsyncConnection conn = new SQLiteAsyncConnection("people.db");

var cityUsers = await conn.QueryAsync<User>(

"SELECT * FROM Users WHERE Name = ?", new object[] { textbox1.Text });

foreach (var user in cityUsers)

{

UserList.ItemsSource = cityUsers;

}

Select all records SQLiteAsyncConnection conn = new SQLiteAsyncConnection("people.db");

bool dbExist = await CheckDbAsync("people.db");

if (!dbExist)

{

await CreateDataBase();

}

var query = conn.Table<User>();

users = await query.ToListAsync();

UserList.ItemsSource = users;

Delete Record private void Button_Click_5(object sender, RoutedEventArgs e)

{

var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "people.db");

using (var db = new SQLite.SQLiteConnection(dbPath))

{ try

{ db.Execute("DELETE FROM Users WHERE Name = ?", textbox1.Text); }

catch (Exception ex)

{ // EXCEPTION HANDLING }}}

Update Record

private void Button_Click_6(object sender, RoutedEventArgs e)

{

var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "people.db");

using (var db = new SQLite.SQLiteConnection(dbPath))

{ try

{ db.Execute("UPDATE Users SET Name = ? WHERE Name = ?", textbox2.Text,textbox1.Text); }

catch (Exception ex)

{ // EXCEPTION HANDLING }}}

Drop Table

XAML: How to do list ? <ListView x:Name="UserList" Margin="671,53,20,20">

<ListView.ItemTemplate>

<DataTemplate>

<StackPanel Margin="10">

<TextBlock Text="{Binding Name}" />

<TextBlock Text="{Binding City}" />

</StackPanel>

</DataTemplate>

</ListView.ItemTemplate>

</ListView>

Summary

Any questions ?

Thank you