Created
November 6, 2017 13:32
-
-
Save brunoportess/4864aecde220ba337260411a0a5429c5 to your computer and use it in GitHub Desktop.
Exemplo implementação sqlite em xamarin forms. Foi usado o nuget package sqlite-net-pcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using NOME_SOLUCAO.Helpers; | |
using NOME_SOLUCAO.Model.Entities; | |
using SQLite; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Xamarin.Forms; | |
namespace NOME_SOLUCAO.SQLite | |
{ | |
public class UnidadePrestadorSQLite : IDisposable | |
{ | |
string _prestadorId = Settings.PrestadorIdSettings; | |
private readonly SQLiteConnection _sqLiteConnection; | |
public UnidadePrestadorSQLite() | |
{ | |
_sqLiteConnection = DependencyService.Get<ISQLiteConf>().GetConnection(); | |
//var config = DependencyService.Get<ISQLiteConf>(); | |
//_sqLiteConnection = new SQLiteConnection(System.IO.Path.Combine(config.DiretorioDB, "bancodados.db3")); | |
_sqLiteConnection.CreateTable<UnidadePrestadorModel>(); | |
} | |
public void InsertAll(List<UnidadePrestadorModel> m) | |
{ | |
//DeleteAll(); | |
//_sqLiteConnection.Query<UsuarioModel>("delete from UnidadePrestadorModel"); | |
// ADD | |
foreach (var u in m) | |
{ | |
_sqLiteConnection.Insert(u); | |
} | |
} | |
public void Update(UnidadePrestadorModel m) | |
{ | |
// UPDATE | |
_sqLiteConnection.Update(new UnidadePrestadorModel | |
{ | |
chavePrestador = "", | |
nm_unidade_atendimento = "" | |
}); | |
} | |
public void Delete(int n) | |
{ | |
// DELETE | |
_sqLiteConnection.Delete<UnidadePrestadorModel>(n); | |
} | |
public void DeleteAll() | |
{ | |
// DELETE | |
_sqLiteConnection.DropTable<UnidadePrestadorModel>(); | |
} | |
public List<UnidadePrestadorModel> GetAll() | |
{ | |
var members = (from mem in _sqLiteConnection.Table<UnidadePrestadorModel>() select mem); | |
return members.ToList(); | |
} | |
public List<UnidadePrestadorModel> GetUnidadePrestador() | |
{ | |
var members = (from mem in _sqLiteConnection.Table<UnidadePrestadorModel>() where mem.chavePrestador == _prestadorId select mem); | |
return members.ToList(); | |
} | |
public UnidadePrestadorModel GetUnidade(string chaveUnidade, string chavePrestador) | |
{ | |
var unidade = (from mem in _sqLiteConnection.Table<UnidadePrestadorModel>() where mem.chaveUnidade.Equals(chaveUnidade) && mem.chavePrestador.Equals(chavePrestador) select mem).FirstOrDefault(); ; | |
return unidade; | |
} | |
public void Dispose() | |
{ | |
_sqLiteConnection.Dispose(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using SQLite; | |
namespace NOME_SOLUCAO.SQLite | |
{ | |
//PCL | |
public interface ISQLiteConf | |
{ | |
SQLiteConnection GetConnection(); | |
string DiretorioDB { get; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using NOME_SOLUCAO.SQLite; | |
using SQLite; | |
using Xamarin.Forms; | |
[assembly: Dependency(typeof(NOME_SOLUCAO.Droid.Helpers.SqliteConf))] | |
namespace NOME_SOLUCAO.Droid.Helpers | |
{ | |
public class SqliteConf : ISQLiteConf | |
{ | |
/*public SQLiteConnection GetConnection() | |
{ | |
}*/ | |
SQLiteConnection ISQLiteConf.GetConnection() | |
{ | |
var dbName = "CustomersDb.db3"; | |
var path = Path.Combine(System.Environment. | |
GetFolderPath(System.Environment. | |
SpecialFolder.Personal), dbName); | |
return new SQLiteConnection(path); | |
} | |
private string _diretorioDB; | |
public string DiretorioDB | |
{ | |
get | |
{ | |
if (string.IsNullOrEmpty(_diretorioDB)) | |
{ | |
_diretorioDB = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); | |
} | |
return _diretorioDB; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment