Skip to content

Commit

Permalink
accesso ai dati locali tramite sqllite
Browse files Browse the repository at this point in the history
  • Loading branch information
maurocavallin committed May 19, 2015
1 parent 4ff3e2a commit 29d48d8
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Modulo4/EsempioAccessoDatiLocali/Droid/SqlLite_Android.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.IO;
using Xamarin.Forms;
using EsempioAccessoDatiLocali.Droid;



[assembly: Dependency (typeof (SQLite_Android))]

/// <summary>
/// https://github.com/xamarin/xamarin-forms-samples/blob/master/Todo/PCL/Todo.Android/SQLite_Android.cs
/// </summary>
namespace EsempioAccessoDatiLocali.Droid
{
public class SQLite_Android : ISQLite
{
public SQLite_Android ()
{
}

#region ISQLite implementation
public SQLite.SQLiteConnection GetConnection ()
{
var sqliteFilename = "TodoSQLite.db3";
string documentsPath = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal); // Documents folder
var path = Path.Combine(documentsPath, sqliteFilename);

// This is where we copy in the prepopulated database
Console.WriteLine (path);

/*
* nel caso dovessi usare un database pre-popolato
if (!File.Exists(path))
{
var s = Forms.Context.Resources.OpenRawResource(Resource.Raw.TodoSQLite); // RESOURCE NAME ###
// create a write stream
FileStream writeStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
// write to the stream
ReadWriteStream(s, writeStream);
}
*/

var conn = new SQLite.SQLiteConnection(path);

// Return the database connection
return conn;
}
#endregion

/// <summary>
/// helper method to get the database out of /raw/ and into the user filesystem
/// </summary>
void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
}
readStream.Close();
writeStream.Close();
}
}
}

7 changes: 7 additions & 0 deletions Modulo4/EsempioAccessoDatiLocali/Droid/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="sqlite-net-pcl" version="1.0.11" targetFramework="MonoAndroid50" />
<package id="SQLitePCL.raw_basic" version="0.7.1" targetFramework="MonoAndroid50" />
<package id="Xamarin.Android.Support.v4" version="22.1.1.1" targetFramework="MonoAndroid50" />
<package id="Xamarin.Forms" version="1.4.2.6359" targetFramework="MonoAndroid50" />
</packages>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Linq;
using SQLite;
using Xamarin.Forms;
using System.Collections.Generic;

namespace EsempioAccessoDatiLocali
{
public class TodoItemDatabase
{
static object locker = new object ();

SQLiteConnection database;

/// <summary>
/// Initializes a new instance of the <see cref="Tasky.DL.TaskDatabase"/> TaskDatabase.
/// if the database doesn't exist, it will create the database and all the tables.
/// </summary>
/// <param name='path'>
/// Path.
/// </param>
public TodoItemDatabase()
{
var db = DependencyService.Get<ISQLite> ();

database = db.GetConnection ();
// create the tables
database.CreateTable<TodoItem>();
}

public IEnumerable<TodoItem> GetItems ()
{
lock (locker) {
return (from i in database.Table<TodoItem>() select i).ToList();
}
}

public IEnumerable<TodoItem> GetItemsNotDone ()
{
lock (locker) {
return database.Query<TodoItem>("SELECT * FROM [TodoItem] WHERE [Done] = 0");
}
}

public TodoItem GetItem (int id)
{
lock (locker) {
return database.Table<TodoItem>().FirstOrDefault(x => x.ID == id);
}
}

public int SaveItem (TodoItem item)
{
lock (locker) {
if (item.ID != 0) {
database.Update(item);
return item.ID;
} else {
return database.Insert(item);
}
}
}

public int DeleteItem(int id)
{
lock (locker) {
return database.Delete<TodoItem>(id);
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using SQLite;

namespace EsempioAccessoDatiLocali
{
public interface ISQLite {

SQLiteConnection GetConnection();

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using SQLite;

namespace EsempioAccessoDatiLocali
{
public class TodoItem
{
public TodoItem ()
{
}

[PrimaryKey, AutoIncrement]
public int ID { get; set; }

public string Name { get; set; }

public string Notes { get; set; }

public bool Done { get; set; }
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="EsempioAccessoDatiLocali.PaginaMenuPrincipale">
<ContentPage.Content
Title="Acceso ai dati locali" Padding="20, 20, 20, 20">

<Grid ColumnSpacing="20" RowSpacing="20">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<Grid Grid.Row="0" Grid.Column="0" BackgroundColor="Aqua"><Button x:Name="PrimoPulsante" Text=""></Button></Grid>
<Grid Grid.Row="0" Grid.Column="1" BackgroundColor="Olive"><Button x:Name="SecondoPulsante" Text=""></Button></Grid>

<Grid Grid.Row="1" Grid.Column="0" BackgroundColor="Olive"><Button x:Name="TerzoPulsante" Text=""></Button></Grid>
<Grid Grid.Row="1" Grid.Column="1" BackgroundColor="Aqua"><Button x:Name="QuartoPulsante" Text=""></Button></Grid>

<Grid Grid.Row="2" Grid.Column="0" BackgroundColor="Aqua"></Grid>
<Grid Grid.Row="2" Grid.Column="1" BackgroundColor="Olive"></Grid>

</Grid>

</ContentPage.Content>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Linq;
using System.Collections.Generic;

using Xamarin.Forms;

using SQLite;

namespace EsempioAccessoDatiLocali
{
public partial class PaginaMenuPrincipale : ContentPage
{
public PaginaMenuPrincipale ()
{
InitializeComponent ();

this.PrimoPulsante.Text = "Conta Elementi";
this.PrimoPulsante.Clicked += (object sender, EventArgs e) => {
contaElementi();
};

this.SecondoPulsante.Text = "Inserisci Elementi";
this.SecondoPulsante.Clicked += (object sender, EventArgs e) => {
inserisciElemento();
};

this.TerzoPulsante.Text = "Elimina Un Elemento";
this.TerzoPulsante.Clicked += (object sender, EventArgs e) => {
eliminaElemento();
};
}

private void contaElementi()
{
var items = App.Database.GetItems ().ToList();

DisplayAlert ("", "Recuperati nro elementi: " + items.Count (), "OK");

}

private void inserisciElemento()
{

TodoItem td = new TodoItem (){
ID = 0, // è nuovo da salvare
Name = "NuovoItem" + DateTime.Now.Ticks,
Notes = "NuovoItemNote" + DateTime.Now.Ticks
};
App.Database.SaveItem (td);

contaElementi ();
}

private void eliminaElemento()
{

var itemToDelete = App.Database.GetItems ().FirstOrDefault ();

if (itemToDelete != null) {
App.Database.DeleteItem (itemToDelete.ID);
} else {
DisplayAlert ("", "non ci sono elementi da cancellare ", "OK");
return;
}

contaElementi ();
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="sqlite-net-pcl" version="1.0.11" targetFramework="portable-net45+win+wp80+MonoAndroid10+xamarinios10+MonoTouch10" />
<package id="SQLitePCL.raw_basic" version="0.7.1" targetFramework="portable-net45+win+wp80+MonoAndroid10+xamarinios10+MonoTouch10" />
<package id="Xamarin.Forms" version="1.4.2.6359" targetFramework="portable-net45+win+wp80+MonoAndroid10+xamarinios10+MonoTouch10" />
</packages>

0 comments on commit 29d48d8

Please sign in to comment.