Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Created March 2, 2016 23:36
Show Gist options
  • Save BrianJVarley/2e36cf10d52efdb7128e to your computer and use it in GitHub Desktop.
Save BrianJVarley/2e36cf10d52efdb7128e to your computer and use it in GitHub Desktop.
using Parking_Tag_Picker_WRT.Models;
using SQLite;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Storage;
namespace Parking_Tag_Picker_WRT.Helpers
{
public class DatabaseHelper
{
/// <summary>
/// Load SQL_LiteTable from Solution
/// </summary>
/// <param name="DB_PATH"></param>
/// <returns></returns>
public async Task<bool> Init(string dbPath)
{
bool isDatabaseExisting = false;
try
{
StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(dbPath);
isDatabaseExisting = true;
}
catch
{
isDatabaseExisting = false;
}
if (!isDatabaseExisting)
{
StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(dbPath);
await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
}
return true;
}
/// <summary>
/// Retrieve the specific zone info from the database.
/// </summary>
/// <param name="zoneId"></param>
/// <returns></returns>
public ZoneInfo ReadZone(int zoneId, string dbPath)
{
using (var dbConn = new SQLiteConnection(dbPath))
{
var existingZone = dbConn.Query<ZoneInfo>("select * from {0} where ObjectId ={1}", dbPath, zoneId).FirstOrDefault();
return existingZone;
}
}
/// <summary>
/// Retrieve zone info list from the database.
/// </summary>
/// <returns></returns>
public ObservableCollection<ZoneInfo> ReadZones(string dbPath)
{
using (var dbConn = new SQLiteConnection(dbPath))
{
List<ZoneInfo> zoneInfo = dbConn.Table<ZoneInfo>().ToList<ZoneInfo>();
ObservableCollection<ZoneInfo> zoneInfoCollection = new ObservableCollection<ZoneInfo>(zoneInfo);
return zoneInfoCollection;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment