-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataAccess.cs
105 lines (92 loc) · 4.56 KB
/
DataAccess.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
using SQLite;
using System.Collections.Generic;
using System.IO;
using Windows.Storage;
namespace PassDefend
{
class DataAccess
{
//declaring layout of list for account information
public class AccountList
{
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public string ID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Notes { get; set; }
}
public static SQLiteConnection OpenDB(string key)
{
string dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "core");
var dboptions = new SQLiteConnectionString(dbpath, true, key: key);
var connection = new SQLiteConnection(dboptions);
return connection;
}
public static void CloseDB(SQLiteConnection connection)
{
connection.Close();
}
//function to initialize and create database and table if it does not already exist
public static void InitializeDatabase(SQLiteConnection connection)
{
//check if table exists
string checkCommand = "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='AccountTable';";
var checkCommandConnection = connection.CreateCommand(checkCommand);
int result = checkCommandConnection.ExecuteScalar<int>();
if (result == 0)
{
string tableCommand = "CREATE TABLE IF NOT " +
"EXISTS AccountTable (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name NVARCHAR(128) NULL, " + "email NVARCHAR(128) NULL, " +
"username NVARCHAR(128) NULL, " + "password NVARCHAR(128) NULL, " +
"notes NVARCHAR(1024) NULL)";
var command = connection.CreateCommand(tableCommand);
command.ExecuteNonQuery();
string formattingCommand = "INSERT INTO AccountTable VALUES (1, null, null, null, null, null);";
command = connection.CreateCommand(formattingCommand);
command.ExecuteNonQuery();
}
}
//function to add a row of data into the database
public static void AddData(SQLiteConnection connection, string name, string email, string username, string password, string notes)
{
string insertCommand = "INSERT INTO AccountTable VALUES (?, ?, ?, ?, ?, ?);";
var tableCommand = connection.CreateCommand(insertCommand, null, name, email, username, password, notes);
tableCommand.ExecuteNonQuery();
}
//function to update rows in the database based on id
public static void UpdateData(SQLiteConnection connection, int id, string name, string email, string username, string password, string notes)
{
string updateCommand = "UPDATE AccountTable SET name = '" + name + "', email = '" + email + "', username = '" + username + "', password = '" + password + "', notes = '" + notes + "' WHERE ID = " + id + ";";
var tableCommand = connection.CreateCommand(updateCommand);
tableCommand.ExecuteNonQuery();
}
//function to delete a row from the database
public static void DeleteData(SQLiteConnection connection, int id)
{
string deleteCommand = "DELETE FROM AccountTable WHERE ID = " + id + ";";
var tableCommand = connection.CreateCommand(deleteCommand);
tableCommand.ExecuteNonQuery();
}
//function to read the information of one specific row.
public static List<AccountList> GetAccountData(SQLiteConnection connection)
{
List<AccountList> account = new List<AccountList>();
string selectCommand = "SELECT * FROM AccountTable";
var accountDataCommand = connection.CreateCommand(selectCommand);
var query = accountDataCommand.ExecuteQuery<AccountList>();
foreach (var readaccount in query)
{
account.Add(new AccountList() { ID = readaccount.ID, Name = readaccount.Name, Email = readaccount.Email, Username = readaccount.Username, Password = readaccount.Password, Notes = readaccount.Notes });
}
return account;
}
//function to update DB password
public static void changeDBPassword(SQLiteConnection connection, string newkey)
{
connection.Execute("PRAGMA rekey = '" + newkey + "';");
}
}
}