Skip to content

Instantly share code, notes, and snippets.

@gavilanch
Last active September 2, 2017 22:20
Show Gist options
  • Save gavilanch/19c38fd23fc3ff2efc24e27d30ad2a85 to your computer and use it in GitHub Desktop.
Save gavilanch/19c38fd23fc3ff2efc24e27d30ad2a85 to your computer and use it in GitHub Desktop.
// Este es el ejemplo utilizado al inicio del video.
private void TestDb()
{
string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string rutaDb = System.IO.Path.Combine(folder, "notiXamarinDb.db");
// Crea la base de datos si no existe, y crea una conexión
var db = new SQLiteConnection(rutaDb);
// Crea la tabla si no existe
db.CreateTable<News>();
var news1 = new News
{
Id = 1,
Title = "titulo 1",
ImageName = "image name 1",
Body = "body 1"
};
var news2 = new News
{
Id = 5,
Title = "Creando apps multiplataforma con Xamarin",
ImageName = "image name 2",
Body = "body 2"
};
var news3 = new News
{
Id = 8,
Title = "Usar Xamarin con c# o desarrollar con Java?",
ImageName = "image name 3",
Body = "body 3"
};
// Inserta elementos en la tabla (uno a uno)
db.Insert(news1);
db.Insert(news2);
db.Insert(news3);
// Reemplaza el elemento si no existe
news1.Title = "nuevo titulo";
db.InsertOrReplace(news1);
// Si queremos insertar varios elementos a la vez
//db.InsertAll(new List<News>() { news1, news2, news3 });
// Obtener una noticia por su Id
var news1_fromDb = db.Get<News>(news1.Id);
// Obtener todas las noticias
var news_todas = db.Table<News>().ToList();
// Obtener un listado de noticias
var news_xmarin = db.Table<News>().Where(x => x.Title.Contains("Xamarin")).ToList();
var cantidadDeNoticias = db.Table<News>().Count();
// Borra el elemento de Id 1 de la tabla
db.Delete<News>(1);
// Borrar todos los elementos de la tabla
db.DeleteAll<News>();
// Borrar la tabla
db.DropTable<News>();
}
// En ValuesService
public class ValuesService
{
public static readonly string ImagesBaseURL = "http://mirepogavilanch2.azurewebsites.net/images/";
public static readonly string DbName = "notiXamarinDb.db";
public static string GetDbPath()
{
string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
return System.IO.Path.Combine(folder, DbName);
}
}
// En newsLocalRepository
internal class NewsLocalRepository
{
private string _dbPath;
public NewsLocalRepository(string dbPath)
{
_dbPath = dbPath;
using (var _db = new SQLiteConnection(_dbPath))
{
_db.CreateTable<News>();
}
}
public void Save(News news)
{
using (var _db = new SQLiteConnection(_dbPath))
{
_db.InsertOrReplace(news);
}
}
public List<News> GetAll()
{
using (var _db = new SQLiteConnection(_dbPath))
{
return _db.Table<News>().ToList();
}
}
public void Delete(int id)
{
using (var _db = new SQLiteConnection(_dbPath))
{
_db.Delete<News>(id);
}
}
public void DeleteAll()
{
using (var _db = new SQLiteConnection(_dbPath))
{
_db.DeleteAll<News>();
}
}
}
// En newsLocalService
public class NewsLocalService
{
private NewsLocalRepository _newsLocalRepository;
public NewsLocalService()
{
_newsLocalRepository = new NewsLocalRepository(ValuesService.GetDbPath());
}
public void Save(News news)
{
_newsLocalRepository.Save(news);
}
public List<News> GetAllSavedForReadLater()
{
return _newsLocalRepository.GetAll();
}
public void Delete(int id)
{
_newsLocalRepository.Delete(id);
}
public void Delete(List<int> ids)
{
ids.ForEach(x => Delete(x));
}
public void DeleteAll()
{
_newsLocalRepository.DeleteAll();
}
}
// en news
public class News
{
[PrimaryKey]
public int Id { get; set; }
public string Title { get; set; }
[Ignore]
public string Body { get; set; }
public string ImageName { get; set; }
}
// en el mainActivity
private void HandleReadLater()
{
try
{
var newsLocalService = new NewsLocalService();
newsLocalService.Save(_news);
Toast.MakeText(this, "Saved", ToastLength.Short).Show();
}
catch (Exception ex)
{
Toast.MakeText(this, "error: " + ex.Message, ToastLength.Long).Show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment