Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Created March 9, 2016 20:37
Show Gist options
  • Select an option

  • Save BrianJVarley/ec382708a877a13ce89d to your computer and use it in GitHub Desktop.

Select an option

Save BrianJVarley/ec382708a877a13ce89d 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.IO;
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
{
public const string AppDBPath = @"ParkingZoneDatabase.db";
public const string PackageDBPath = @"Databases\ParkingZoneDatabase.db";
/// <summary>
/// Load SQL_LiteTable from Solution
/// </summary>
/// <param name="DBPATH"></param>
/// <returns></returns>
public async Task<bool> Init()
{
bool isDatabaseExisting = false;
try
{
StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(AppDBPath);
isDatabaseExisting = true;
}
catch
{
isDatabaseExisting = false;
}
if (!isDatabaseExisting)
{
StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(PackageDBPath);
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 tableName)
{
using (var dbConn = new SQLiteConnection(tableName))
{
var existingZone = dbConn.Query<ZoneInfo>("select * from {0} where ObjectId ={1}", tableName, zoneId).FirstOrDefault();
return existingZone;
}
}
/// <summary>
/// Retrieve zone info list from the database.
/// </summary>
/// <returns></returns>
public ObservableCollection<ZoneInfo> ReadZones(string tableName)
{
using (var dbConn = new SQLiteConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, AppDBPath), true))
{
List<ZoneInfo> zoneInfo = dbConn.Query<ZoneInfo>("select * from " + tableName).ToList<ZoneInfo>();
ObservableCollection<ZoneInfo> zoneInfoCollection = new ObservableCollection<ZoneInfo>(zoneInfo);
return zoneInfoCollection;
}
}
}
}
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Parking_Tag_Picker_WRT.Models
{
public class ZoneInfo
{
//The ObjectId property is marked as the Primary Key
[SQLite.PrimaryKey]
[Column("results_list_objectId")]
public string ObjectId { get; set; }
[Column("results_list_zone")]
public string ZoneName { get; set; }
[Column("results_list_tariff_ph")]
public int? TariffPH { get; set; }
[Column("results_list_tariff_pd")]
public int? TariffPD { get; set; }
[Column("results_list_restrictions")]
public string Restrictions { get; set; }
[Column("results_list_days_of_operation")]
public string DaysOpen { get; set; }
[Column("results_list_hours_of_operation")]
public string HoursOpen { get; set; }
public ZoneInfo()
{
}
public ZoneInfo(string objectId, string zoneName, int tariffPH, int tariffPD,
string restrictions, string daysOpen, string hoursOpen )
{
ObjectId = objectId;
ZoneName = zoneName;
TariffPH = tariffPH;
TariffPD = tariffPD;
Restrictions = restrictions;
DaysOpen = daysOpen;
HoursOpen = hoursOpen;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment