-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhraseDB.cs
131 lines (129 loc) · 4.56 KB
/
PhraseDB.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLite;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
namespace HangingMan
{
public class PhraseDB
{
public const string dbName = "HangingManDB.db"; //Name of All DB that contains all tables
public const string tabName = "Phrase"; //Name of table - same as class
// Name of folder in smartphone where the DB is created
public string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
//this function createsx the DB if it is not already exist
public bool CreateDatabase<T>()
{
try
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, dbName)))
{
connection.CreateTable<T>();
return true;
}
}
catch (SQLiteException ex)
{ // if the DB was not created for some rerason a message is logged and the function returns false;
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
//Add or Insert Operation - operaters insert qsql query
// data = relevant record without id column.
public bool InsertIntoTable<T>(T data)
{
try
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, dbName)))
{
connection.Insert(data);
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
// This function returns a list od all Mission Data acoording SQL to statement
// The statement should be modified according to your app needs.
public List<Phrase> SelectTableData()
{
List<Phrase> listall = new List<Phrase>();
try
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, dbName)))
{
//Modify the sql statement to retrieve the releveant data.
listall = connection.Query<Phrase>("SELECT * FROM " + tabName + " order by missionName").ToList();
return listall;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return null;
}
}
//Example of fetching relevant data by name
public List<Phrase> SelectByName(string name)
{
List<Phrase> listbyname = new List<Phrase>();
try
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, dbName)))
{
listbyname = connection.Query<Phrase>("SELECT * FROM " + tabName + " Where missionName=?", name).ToList();
return listbyname;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return null;
}
}
//Activate update statment on DB - must send full mission record with id.
public bool UpdateTable(Phrase mis)
{
try
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, dbName)))
{
connection.Update(mis);
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
//activate delet sql statement according to id
public bool DeleteRow(long id)
{
try
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, dbName)))
{
connection.Query<Phrase>("Delete from " + tabName + " Where Id=?", id);
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
}
}