Final Term Lab Final
Final Term Lab Final
To keep record of the books issued, BookIssues table will have the following attributes:
Book Id, Member Id, Issue Data, Expected Return Date, Is Returned
For a member, the database table Members will have the following attributes:
Member Id, Member Name, Address
Classes
You will need to define the following classes:
Book, Member, BookIssue - for book, member and book issue objects
Library - It will implement the logic of business rules and will be between the user interface and
database class.
Database - It will implement the functionality of saving and retrieving data from the SQLite database.
Business Rules
A member will not be issued book if he has already borrowed one and has not returned it as yet.
A book will not be issued if its available copies are 0.
A book can have a maximum of 10 copies.
Starter Code
Following is the code to connect to an SQLite3 DB:
You need to declare a member of the Database class:
sqlite3 *db;
const char* dbPath = “---write your DB path here---”;
if (rc != SQLITE_DONE) {
std::cerr << "Record not entered becuase of : " << sqlite3_errmsg(db) << std::endl;
}
//release resources
sqlite3_finalize(stmt);
Following is the code to retrieve two column values from a table based on a condition:
const char* sql = "SELECT Title, TotalCopies FROM Books WHERE AvailableCopies < ?;";
sqlite3_stmt* stmt;
std::vector<std::pair<std::string, int>> retrieved_books;
int available_value{5};
sqlite3_bind_int(stmt, 1, available_value);
//use the following variation if the condition is a string variable (e.g., publisher):
//sqlite3_bind_text(stmt, 1, publisher.c_str(), -1, SQLITE_STATIC);
if (rc != SQLITE_DONE) {
std::cerr << "Execution failed: " << sqlite3_errmsg(db) << std::endl;
}
sqlite3_finalize(stmt);
Following code prints the values retrieved from the DB (and saved int a vector of pairs):
for (const auto& book : retrieved_books) {
std::cout << "Title: " << book.first << ", Total Copies: " << book.second << std::endl;
}
Starter Code
Following is the code to connect to the database:
#it will create an object named conn to connect to the DB
self.conn = sqlite3.connect(dbPath)
#it will create a cursor object associated with the conn object.
self.cur = self.conn.cursor()
#at the end you need to call the commit() method of the conn object.
self.conn.commit()
self.conn.commit()
except sqlite3.Error as e:
#handle if any run time error occurs