· 7 years ago · Jan 10, 2019, 10:58 AM
1using System;
2using System.Data.SQLite;
3using System.IO;
4
5namespace CursApp.Library
6{
7 public class LogDB
8 {
9 private readonly string fileName = "log.db";
10 private SQLiteConnection connection;
11 private SQLiteCommand command;
12 private static LogDB _instance;
13
14 private LogDB()
15 {
16 string path = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, fileName);
17
18 if (!File.Exists(path))
19 {
20 SQLiteConnection.CreateFile(path);
21 }
22
23 connection = new SQLiteConnection("Data Source=" + path + ";Version=3;");
24 command = new SQLiteCommand(connection);
25 connection.Open();
26
27 command.CommandText = "CREATE TABLE IF NOT EXISTS Logs (id INTEGER PRIMARY KEY AUTOINCREMENT, date TEXT, message TEXT)";
28 command.ExecuteNonQuery();
29
30 }
31
32 public static LogDB GetInstance()
33 {
34 if (_instance == null)
35 {
36 _instance = new LogDB();
37 }
38
39 return _instance;
40 }
41
42 public void WriteToDB(string message)
43 {
44 command.CommandText = $"INSERT INTO Logs ('date', 'message') VALUES ('{DateTime.Now.ToString()}', '{message}')";
45 command.ExecuteNonQuery();
46 }
47 }
48}