-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSQLiteHelper.cs
More file actions
55 lines (48 loc) · 1.85 KB
/
Copy pathSQLiteHelper.cs
File metadata and controls
55 lines (48 loc) · 1.85 KB
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
using System.Data.SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace password_manager
{
public class SQLiteHelper
{
public SQLiteConnection CreateConnection()
{
string dbFilePath = "database.db";
if (!System.IO.File.Exists(dbFilePath))
{
SQLiteConnection.CreateFile(dbFilePath);
}
string connectionString = $"Data Source={dbFilePath};Version=3;";
return new SQLiteConnection(connectionString);
}
// Create needed tables if they don't exist
public void CreateTables()
{
using (SQLiteConnection connection = CreateConnection())
{
connection.Open();
using (SQLiteCommand command = new SQLiteCommand())
{
command.Connection = connection;
command.CommandText = @"
CREATE TABLE IF NOT EXISTS service (
id INTEGER PRIMARY KEY AUTOINCREMENT,
service_name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS account (
id INTEGER PRIMARY KEY AUTOINCREMENT,
login TEXT NOT NULL,
password_hash BLOB NOT NULL,
service_id INTEGER NOT NULL,
FOREIGN KEY (service_id) REFERENCES service(id)
);";
command.ExecuteNonQuery();
}
connection.Close();
}
}
}
}