Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Language: C++ Overview The Final Project is to develop a simple database system.

ID: 3812390 • Letter: L

Question

Language: C++
Overview
The Final Project is to develop a simple database system. The database is to handle multiple records, each composed of several fields. The database will store its information to a file, addition and deletion of records, field modifications, and it will allow users to sort records based on the selected keys, and produce reports (output) according to predefined criteria.
Language: C++
Overview
The Final Project is to develop a simple database system. The database is to handle multiple records, each composed of several fields. The database will store its information to a file, addition and deletion of records, field modifications, and it will allow users to sort records based on the selected keys, and produce reports (output) according to predefined criteria.
Language: C++
Overview
The Final Project is to develop a simple database system. The database is to handle multiple records, each composed of several fields. The database will store its information to a file, addition and deletion of records, field modifications, and it will allow users to sort records based on the selected keys, and produce reports (output) according to predefined criteria.

Explanation / Answer

CREATE DATABASE business_data;

CREATE TABLE `business_data`.`student` (
`student_id` BIGINT NOT NULL AUTO_INCREMENT,
`student_name` VARCHAR(255) NOT NULL,
`class_name` VARCHAR(45) NOT NULL,  
`phone` BIGINT(15) NULL DEFAULT NULL,
PRIMARY KEY (`student_id`))
ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO student(student_id, student_name, class_name, phone) VALUES(1, 'Zubaeyr', 'CS101', 9177042711);
INSERT INTO student(student_id, student_name, class_name, phone) VALUES(2, 'Azhar', 'CS102', 0981622711);
INSERT INTO student(student_id, student_name, class_name, phone) VALUES(3, 'Pramela', 'CS103', 00966654212);
INSERT INTO student(student_id, student_name, class_name, phone) VALUES(4, 'Sumair', 'CS104', 9618676960);

---------------------------------------------------------------------------------------------------------------------------

/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>

/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

using namespace std;

void printAllRecords(sql::Connection *con, string sort_attrib);
void deleteStudentRecord(sql::Connection *con, string student_id);
void updateStudentRecord(sql::Connection *con, string student_id, string student_name, string class_name, string phone);

int main(void)
{

   try {
      sql::Driver *driver;
      sql::Connection *connection;

      driver = get_driver_instance();
      connection = driver->connect("tcp://127.0.0.1:3306", "root", "root");
      connection->setSchema("business_data");

      /* Print all the records
       */
      printAllRecords(connection, "");
      updateStudentRecord(connection, "2", "Oberyn", "CS098", "4410508");
      printAllRecords(connection, "student_name");
      deleteStudentRecord(connection, "3");
      printAllRecords(connection, "student_id");

      delete connection;
  
   } catch (sql::SQLException &e) {
      cout << "# ERR: SQLException in " << __FILE__ << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
      cout << "# ERR: " << e.what() << " (MySQL error code: " << e.getErrorCode();
      cout << ", SQLState: " << e.getSQLState() << " )" << endl;
   }

   cout << endl;
  
   return EXIT_SUCCESS;
}

void printAllRecords(sql::Connection *con, string sort_attrib)
{
      if (sort_attrib.empty()) {
       sort_attrib = "student_id";
      }
      sql::Statement *stmt = con->createStatement();
      sql::ResultSet *res = stmt->executeQuery("SELECT * FROM student ORDER BY "+sort_attrib+";");

      cout << "Student Data " <<endl;
      while (res->next()) {
        cout << res->getString("student_id") << ", ";  
        cout << res->getString("student_name") << ", ";  
        cout << res->getString("class_name") << ", ";  
        cout << res->getString("phone") <<endl;  
      }
      delete stmt;
      delete res;
}

void updateStudentRecord(sql::Connection *con, string student_id, string student_name, string class_name, string phone)
{
      sql::Statement *stmt = con->createStatement();
      cout << endl << "updating record with student_id = " << student_id << endl << endl;
      stmt->executeUpdate("UPDATE student SET student_name = '" + student_name + "', class_name = '" + class_name + "', phone = " + phone + " WHERE student_id = " + student_id + ";");
      delete stmt;
}

void deleteStudentRecord(sql::Connection *con, string student_id)
{
      sql::Statement *stmt = con->createStatement();
      cout << endl << "deleting record with student_id = " << student_id << endl << endl;
      stmt->executeUpdate("DELETE FROM student where student_id = " + student_id + ";");
      delete stmt;
}