using System;
using System.Data.SQLite;
using System.IO;
class BookDataHandler
{
private string connectionString;
public BookDataHandler(string connectionString)
{
this.connectionString = connectionString;
}
public void CreateTableIfNotExists()
{
string createTableQuery = @"CREATE TABLE IF NOT EXISTS Books (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Title TEXT NOT NULL,
Author TEXT NOT NULL,
Genre TEXT NOT NULL,
Summary TEXT NOT NULL);";
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
conn.Open();
using (SQLiteCommand cmd = new SQLiteCommand(createTableQuery, conn))
{
cmd.ExecuteNonQuery();
}
}
}
public void InsertBook(string title, string author, string genre, string summary)
{
string insertQuery = "INSERT INTO Books (Title, Author, Genre, Summary) VALUES (@Title, @Author, @Genre, @Summary);";
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
conn.Open();
using (SQLiteCommand cmd = new SQLiteCommand(insertQuery, conn))
{
cmd.Parameters.AddWithValue("@Title", title);
cmd.Parameters.AddWithValue("@Author", author);
cmd.Parameters.AddWithValue("@Genre", genre);
cmd.Parameters.AddWithValue("@Summary", summary);
cmd.ExecuteNonQuery();
}
}
}
public void DisplayBooks()
{
string query = "SELECT * FROM Books;";
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
conn.Open();
using (SQLiteCommand cmd = new SQLiteCommand(query, conn))
{
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
Console.WriteLine("Books in the database:");
Console.WriteLine("-------------------------------------");
while (reader.Read())
{
Console.WriteLine($"ID: {reader["Id"]}");
Console.WriteLine($"Title: {reader["Title"]}");
Console.WriteLine($"Author: {reader["Author"]}");
Console.WriteLine($"Genre: {reader["Genre"]}");
Console.WriteLine($"Summary: {reader["Summary"]}");
Console.WriteLine("-------------------------------------");
}
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
string databaseFile = "Books.db";
string connectionString = $"Data Source={databaseFile};Version=3;";
if (!File.Exists(databaseFile))
{
Console.WriteLine("Database file does not exist. A new database will be created.");
}
BookDataHandler dataHandler = new BookDataHandler(connectionString);
dataHandler.CreateTableIfNotExists();
while (true)
{
Console.WriteLine("1. Add a new book");
Console.WriteLine("2. View all books");
Console.WriteLine("3. Exit");
Console.Write("Enter your choice: ");
string choice = Console.ReadLine();
if (choice == "1")
{
Console.Write("Enter the book title: ");
string title = Console.ReadLine();
Console.Write("Enter the author: ");
string author = Console.ReadLine();
Console.Write("Enter the genre: ");
string genre = Console.ReadLine();
Console.Write("Enter the summary: ");
string summary = Console.ReadLine();
dataHandler.InsertBook(title, author, genre, summary);
Console.WriteLine("Book added successfully!");
}
else if (choice == "2")
{
dataHandler.DisplayBooks();
}
else if (choice == "3")
{
break;
}
else
{
Console.WriteLine("Invalid choice. Please try again.");
}
}
}
}