-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibraryDatabase.cs
92 lines (79 loc) · 3.32 KB
/
LibraryDatabase.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
// DON-CODE
using System;
using System.Data.SQLite;
using System.IO;
public class LibraryDatabase {
private string connectionString;
public LibraryDatabase(string databasePath) {
if (string.IsNullOrWhiteSpace(databasePath)) {
throw new ArgumentNullException(nameof(databasePath), "Database path cannot be null or empty.");
}
connectionString = $"Data Source={databasePath};Version=3;";
InitializeDatabase();
}
private void InitializeDatabase() {
using (SQLiteConnection connection = new SQLiteConnection(connectionString)) {
connection.Open(); // Open the DB connection
// Create tables
string createBooksTableSql = @"
CREATE TABLE IF NOT EXISTS Books (
BookID INTEGER PRIMARY KEY AUTOINCREMENT,
Title TEXT NOT NULL,
Author TEXT,
ISBN TEXT UNIQUE,
Quantity INTEGER,
AvailableQuantity INTEGER,
Genre TEXT,
PublicationYear TEXT,
Publisher TEXT,
Language TEXT,
Description TEXT,
Location TEXT
);
";
// ISBN - International Standard Book Number
string createMembersTableSql = @"
CREATE TABLE IF NOT EXISTS Members (
MemberID INTEGER PRIMARY KEY AUTOINCREMENT,
FirstName TEXT NOT NULL,
LastName TEXT NOT NULL,
Email TEXT UNIQUE,
PhoneNumber TEXT
);
";
string createLendingTableSql = @"
CREATE TABLE IF NOT EXISTS Lending (
TransactionID INTEGER PRIMARY KEY AUTOINCREMENT,
BookID INTEGER NOT NULL,
MemberID INTEGER NOT NULL,
LendDate TEXT NOT NULL,
DueDate TEXT NOT NULL,
ReturnDate TEXT DEFAULT 'N/A',
FOREIGN KEY (BookID) REFERENCES Books (BookID),
FOREIGN KEY (MemberID) REFERENCES Members (MemberID)
);
";
string createFinesTableSql = @"
CREATE TABLE IF NOT EXISTS Fines (
FineID INTEGER PRIMARY KEY AUTOINCREMENT,
TransactionID INTEGER NOT NULL,
Amount DECIMAL(5, 2) NOT NULL,
FOREIGN KEY (TransactionID) REFERENCES Lending (TransactionID)
);
";
using (SQLiteCommand command = new SQLiteCommand(createBooksTableSql, connection)) {
command.ExecuteNonQuery();
}
using (SQLiteCommand command = new SQLiteCommand(createMembersTableSql, connection)) {
command.ExecuteNonQuery();
}
using (SQLiteCommand command = new SQLiteCommand(createLendingTableSql, connection)) {
command.ExecuteNonQuery();
}
using (SQLiteCommand command = new SQLiteCommand(createFinesTableSql, connection)) {
command.ExecuteNonQuery();
}
connection.Close(); //Close the DB connection
}
}
}