50 lines
1.7 KiB
C#
50 lines
1.7 KiB
C#
using Microsoft.Data.Sqlite;
|
|
using Project.Model;
|
|
namespace Project.Persistence;
|
|
|
|
public class SQLite
|
|
{
|
|
private static string _dbPath = "C:/Users/Soi/Project_Keywi/logins.db";
|
|
//KLASSENVARIABLEN ERSTELLEN
|
|
//private static string loginname;
|
|
|
|
|
|
//checks if there is a sqlite database in _dbPath on programm startup, if not -> creates the database and a table LOGINS
|
|
public static void Init()
|
|
{
|
|
using var connection = new SqliteConnection($"Data Source={_dbPath}");
|
|
connection.Open();
|
|
|
|
var command = connection.CreateCommand();
|
|
command.CommandText =
|
|
@"
|
|
CREATE TABLE IF NOT EXISTS LOGINS (
|
|
loginname STRING PRIMARY KEY NOT NULL,
|
|
loginpass STRING NOT NULL,
|
|
loginemail STRING NOT NULL
|
|
);
|
|
";
|
|
command.ExecuteNonQuery();
|
|
}
|
|
|
|
//saves a new user to the table LOGINS ----- no input sanitization yet
|
|
public static void SaveUser(NewUser user)
|
|
{
|
|
using var connection = new SqliteConnection($"Data Source={_dbPath}");
|
|
connection.Open();
|
|
connection.Open();
|
|
using (var command = connection.CreateCommand())
|
|
{
|
|
command.Parameters.AddWithValue("@loginname", user.NewLoginName);
|
|
command.Parameters.AddWithValue("@loginpass", user.NewMasterPassword);
|
|
command.Parameters.AddWithValue("@loginemail", user.NewEmail);
|
|
|
|
command.CommandText =
|
|
@"
|
|
INSERT INTO LOGINS (loginname, loginpass, loginemail)
|
|
VALUES (@loginname, @loginpass, @loginemail);";
|
|
command.ExecuteNonQuery();
|
|
|
|
}
|
|
}
|
|
} |