diff --git a/Src/Mini_Database_Engine/database.cpp b/Src/Mini_Database_Engine/database.cpp new file mode 100644 index 0000000..23f256d --- /dev/null +++ b/Src/Mini_Database_Engine/database.cpp @@ -0,0 +1,280 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +class Record { +public: + vector values; + Record(const vector& vals) : values(vals) {} + void display() const{ + for (size_t i = 0; i < values.size(); i++) + cout << values[i] << "\t"; + cout << endl; + } +}; + +class Table { +private: + string name; + vectorcolumns; + vectorrecords; + +public: + Table() { + + } + Table(const string& tableName, const vector& cols) : name(tableName), columns(cols) { + + } + + const string& getName() const { return name; } + const vector& getColumns() const { return columns; + } + + void insertRecord(const vector& values) { + if (values.size() != columns.size()) { + cerr << "Error: Column count mismatch!" << endl; + return; + } + records.emplace_back(values); + cout << "Record inserted successfully.\n"; + } + + void displayAll() const { + cout << "\nTable: " << name << endl; + for (size_t i = 0; i < columns.size(); i++) + cout << columns[i] << "\t"; + cout << "\n--------------------------\n"; + for (size_t i = 0; i < records.size(); i++) + records[i].display(); + } + + void deleteRecord(int index) { + if (index < 0 || index >= (int)records.size()) { + cerr << "Error: Invalid record index." << endl; + return; + } + records.erase(records.begin() + index); + cout << "Record deleted successfully.\n"; + } + + void updateRecord(int index, const vector& newValues) { + if (index < 0 || index >= (int)records.size()) { + cerr << "Error: Invalid record index." << endl; + return; + } + if (newValues.size() != columns.size()) { + cerr << "Error: Column count mismatch!" << endl; + return; + } + records[index] = Record(newValues); + cout << "Record updated successfully.\n"; + } + + void query(const string& column, const string& value) const { + int colIndex = -1; + for (size_t i = 0; i < columns.size(); i++) { + if (columns[i]==column) { colIndex = i; break; } + } + if (colIndex == -1) { cerr << "Error: Column not found!" << endl; return; } + + cout << "\nQuery Results for "<< column<< " = " << value << ":\n"; + for (size_t i = 0; i < records.size(); i++) { + if (records[i].values[colIndex] == value) + records[i].display(); + } + } + + void saveToFile(ofstream& out) const { + out << name << "\n" << columns.size() << "\n"; + for (size_t i = 0; i < columns.size(); i++) out << columns[i] << "\n"; + out << records.size() << "\n"; + for (size_t i = 0; i < records.size(); i++) { + for (size_t j = 0; j < records[i].values.size(); j++) + out << records[i].values[j] << "\n"; + } + } + + void loadFromFile(ifstream& in) { + size_t colCount, recCount; + in >> colCount; + in.ignore(numeric_limits::max(),'\n'); + columns.clear(); + for (size_t i = 0; i < colCount; i++) { + string col; + getline(in, col); + columns.push_back(col); + } + + in >> recCount; + in.ignore(numeric_limits::max(),'\n'); + records.clear(); + for (size_t i = 0; i < recCount; i++) { + vector vals; + for (size_t j = 0; j < colCount; j++) { + string val; + getline(in, val); + vals.push_back(val); + } + records.emplace_back(vals); + } + } +}; + +class Database { +private: + unordered_map tables; + +public: + void createTable(const string& name, const vector& columns) { + if (tables.find(name) != tables.end()) { cerr << "Error: Table already exists!" << endl; return; } + tables[name] = Table(name, columns); + cout << "Table '" << name << "' created successfully.\n"; + } + + Table* getTable(const string& name) { + auto it = tables.find(name); + if (it == tables.end()) { cerr << "Error: Table not found!" << endl; return nullptr; } + return &(it->second); + } + + void saveDatabase(const string& filename)const { + ofstream out(filename.c_str()); + if (!out) { cerr << "Error: Cannot open file for saving.\n"; return; } + out << tables.size() << "\n"; + for (auto it = tables.begin(); it != tables.end(); ++it) + it->second.saveToFile(out); + cout << "Database saved to " << filename << endl; + } + + void loadDatabase(const string& filename) { + ifstream in(filename.c_str()); + if (!in) { cerr << "No previous database found. Starting fresh.\n"; return; } + size_t tableCount; + in >> tableCount; in.ignore(numeric_limits::max(), '\n'); + tables.clear(); + for (size_t i = 0; i < tableCount; i++) { + string tableName; + getline(in, tableName); + Table t; + t.loadFromFile(in); + tables[tableName] = t; + } + cout << "Database loaded from " << filename << endl; + } +}; + +int main() { + Database db; + db.loadDatabase("database.txt"); + + int choice; + while (true) { + cout << "\n----- Mini Database Engine -----\n"; + cout << "1. Create Table\n2. Insert Record\n3. Display Table\n4. Delete Record\n"; + cout << "5. Update Record\n6. Query Data\n7. Save & Exit\nEnter choice: "; + cin >> choice; + cin.ignore(numeric_limits::max(), '\n'); + if (choice == 1) { + string name; + int cols; + cout << "Enter table name: "; + getline(cin, name); + cout << "Enter number of columns: "; + cin >> cols; + cin.ignore(numeric_limits::max(), '\n'); + vector columns(cols); + cout << "Enter column names (one per line):\n"; + for (int i = 0; i < cols; i++) + getline(cin, columns[i]); + + db.createTable(name, columns); + } + else if (choice == 2) { + string tname; + cout << "Enter table name: "; + getline(cin, tname); + Table* t = db.getTable(tname); + if (t) { + vector vals; + for (size_t i = 0; i < t->getColumns().size(); i++) { + string val; + cout << t->getColumns()[i] << ": "; + getline(cin, val); + vals.push_back(val); + } + t->insertRecord(vals); + } + } + else if (choice == 3) { + string tname; + cout << "Enter table name: "; + getline(cin, tname); + Table* t = db.getTable(tname); + if (t) t->displayAll(); + } + else if (choice == 4) { + string tname; + int index; + cout<< "Enter table name: "; + getline(cin, tname); + cout<< "Enter record index (starting from 0): "; + cin>> index; + cin.ignore(numeric_limits::max(), '\n'); + Table* t = db.getTable(tname); + if (t) t->deleteRecord(index); + } + else if (choice == 5) { + string tname; + int index; + cout << "Enter table name: "; + getline(cin, tname); + Table* t = db.getTable(tname); + if (t) { + cout<< "Enter record index: "; + cin>> index; + cin.ignore(numeric_limits::max(), '\n'); + + vector newVals; + for (size_t i = 0; i < t->getColumns().size(); i++) { + string val; + cout<< t->getColumns()[i] << ": "; + getline(cin, val); + newVals.push_back(val); + } + t->updateRecord(index, newVals); + } + } + else if (choice == 6) { + string tname, col, val; + cout<< "Enter table name: "; + getline(cin, tname); + cout<< "Enter column to search: "; + getline(cin, col); + cout<< "Enter value: "; + getline(cin, val); + Table* t = db.getTable(tname); + if (t) t->query(col, val); + } + else if (choice == 7) { + db.saveDatabase("database.txt"); + cout << "Exiting...\n"; + break; + } + else cout << "Invalid choice. Try again.\n"; + } + + return 0; +} +#ifdef _WIN32 +#include +int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { + return main(); +} +#endif diff --git a/Src/Mini_Database_Engine/output/database.exe b/Src/Mini_Database_Engine/output/database.exe new file mode 100644 index 0000000..171793c Binary files /dev/null and b/Src/Mini_Database_Engine/output/database.exe differ diff --git a/Src/Mini_Database_Engine/output/database.txt b/Src/Mini_Database_Engine/output/database.txt new file mode 100644 index 0000000..26754fe --- /dev/null +++ b/Src/Mini_Database_Engine/output/database.txt @@ -0,0 +1,13 @@ +2 +ss +2 +s +s +0 +Student +2 +Name +Class +1 +Shreya +2