/
HistoryDatabase.cs
103 lines (97 loc) · 3.33 KB
/
HistoryDatabase.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
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace SIP_Notifier
{
class HistoryDatabase
{
private static HistoryDatabase dbInstance;
private static string databaseName = @".\history.db";
private readonly SQLiteConnection conn = new SQLiteConnection("Data Source=" + databaseName + "; Version=3;");
private HistoryDatabase() {
InitializeDB();
}
private void InitializeDB()
{
if (!File.Exists(databaseName))
{
try
{
SQLiteConnection.CreateFile(databaseName);
string sql = "create table history (id INTEGER PRIMARY KEY, date timestamp DEFAULT CURRENT_TIMESTAMP, phone VARCHAR(15), text VARCHAR(2000))";
conn.Open();
SQLiteCommand command = new SQLiteCommand(sql, conn);
command.ExecuteNonQuery();
}
catch (SQLiteException e)
{
Debug.WriteLine("Not connected : " + e.ToString());
}
finally
{
Debug.WriteLine("End..");
conn.Close();
}
}
Debug.WriteLine(File.Exists(databaseName) ? "База данных создана" : "Возникла ошиюка при создании базы данных");
}
public static HistoryDatabase getInstance() {
if(dbInstance == null)
{
dbInstance = new HistoryDatabase();
}
return dbInstance;
}
public void Add(string phone, string text)
{
Debug.WriteLine("Update history : " + text);
try
{
string sql = "insert into history (phone, text) values ('" + phone + "', '" + text + "')";
conn.Open();
SQLiteCommand command = new SQLiteCommand(sql, conn);
command.ExecuteNonQuery();
}
catch (SQLiteException e)
{
Debug.WriteLine("Not connected : " + e.ToString());
}
finally
{
Debug.WriteLine("End..");
conn.Close();
}
}
public List<HistoryRow> getAll()
{
List<HistoryRow> list = new List<HistoryRow>();
Debug.WriteLine("Read history");
try
{
string sql = "select * from history order by date asc";
conn.Open();
SQLiteCommand command = new SQLiteCommand(sql, conn);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
list.Add(new HistoryRow(reader.GetInt16(0), reader.GetString(3), reader.GetString(2), reader.GetString(1)));
}
}
catch (SQLiteException e)
{
Debug.WriteLine("Not connected : " + e.ToString());
return list;
}
finally
{
Debug.WriteLine("End..");
conn.Close();
}
return list;
}
}
}