· 8 years ago · Mar 13, 2018, 01:30 AM
1string path = Path.Combine(FilePaths.Data, Properties.Settings.Default.DatabaseName);
2 if (!File.Exists(path))
3 {
4 SQLiteConnection.CreateFile(path);
5 Log.Info("Database alexandriadb.sqlite not found! Creating now in " + path);
6 }
7 SQLiteConnectionStringBuilder b = new SQLiteConnectionStringBuilder();
8 b.DataSource = path;
9 Properties.Settings.Default.DatabaseConnectionString = b.ConnectionString;
10 Properties.Settings.Default.Save();
11 using (var conn = new SQLiteConnection(Properties.Settings.Default.DatabaseConnectionString))
12 {
13 conn.Open();
14 if (new SQLiteCommand(
15 @"CREATE TABLE IF NOT EXISTS books
16 (id INTEGER NOT NULL,
17 title TEXT(255) NOT NULL,
18 author TEXT(255) NOT NULL,
19 PRIMARY KEY(id));",
20 conn).ExecuteNonQuery() > 0)
21 Log.Info("Creating books table");
22 if (new SQLiteCommand(
23 @"CREATE TABLE IF NOT EXISTS users
24 (id INTEGER DEFAULT 1 NOT NULL,
25 name TEXT(255) NOT NULL,
26 email TEXT(320) NOT NULL,
27 type INTEGER(1) DEFAULT 0 NOT NULL,
28 fines INTEGER default 0,
29 PRIMARY KEY (id));",
30 conn).ExecuteNonQuery() > 0)
31 Log.Info("Creating books table");
32 if (new SQLiteCommand(
33 @"CREATE TABLE IF NOT EXISTS holds
34 (id INTEGER NOT NULL,
35 id_book INTEGER NOT NULL,
36 id_user INTEGER NOT NULL,
37 checked DATETIME DEFAULT (DATETIME('now', 'localtime')) NOT NULL,
38 due DATETIME,
39 returned DATETIME,
40 PRIMARY KEY (id),
41 FOREIGN KEY (id_book) REFERENCES books(id) ON DELETE CASCADE);",
42 conn).ExecuteNonQuery() > 0)
43 Log.Info("Creating books table");
44 }