· 8 years ago · Dec 13, 2017, 08:00 PM
1 public class SQLiteHandler
2 {
3 private SQLiteConnection connection = null;
4
5 public SQLiteHandler()
6 {
7 Trace.Listeners.Add(new TextWriterTraceListener("Exceptions.log"));
8 Trace.AutoFlush = true;
9 }
10
11
12 public void connectToDB()
13 {
14 connection = new SQLiteConnection("Data Source=Database.sqlite;Version = 3; ");
15 connection.Open();
16
17
18 if (!File.Exists("Database.sqlite"))
19 {
20 connection = new SQLiteConnection("Data Source=Database.sqlite;Version = 3; ");
21 connection.Open();
22 }
23
24 try
25 {
26 connection = new SQLiteConnection("Data Source=Database.sqlite;Version = 3; ");
27 connection.Open();
28 }
29 catch (SQLiteException ex)
30 {
31 Trace.Write(DateTime.Now.TimeOfDay + "-->");
32 Trace.Indent();
33 Trace.WriteLine(ex);
34 Trace.Unindent();
35 }
36
37 string comm = "CREATE TABLE IF NOT EXISTS users (" +
38 "nume TEXT," +
39 "prenume TEXT," +
40 "adresa TEXT," +
41 "cnp TEXT);";
42
43 SQLiteCommand command = new SQLiteCommand(comm, connection);
44
45 try
46 {
47 command.ExecuteNonQuery();
48 }
49 catch (SQLiteException ex)
50 {
51 Trace.Write(DateTime.Now.TimeOfDay + "-->");
52 Trace.Indent();
53 Trace.WriteLine(ex);
54 Trace.Unindent();
55 }
56
57 string alterTable = "ALTER TABLE users ADD COLUMN pwd TEXT;";
58
59 SQLiteCommand alterCommand = new SQLiteCommand(alterTable, connection);
60
61 try
62 {
63 alterCommand.ExecuteNonQuery();
64 }
65 catch (SQLiteException ex)
66 {
67 Trace.Write(DateTime.Now.TimeOfDay + "-->");
68 Trace.Indent();
69 Trace.WriteLine(ex);
70 Trace.Unindent();
71 }
72 }