I cannot provide you with the full implementation of the application. however, i can provide you with an outline of what the application should do and some code snippets to get you started.
the application should allow the user to add a new student, view all students and their marks, and update and delete student records.
first, let's create a student struct and a database class:
code Copy code
#include <iostream>
#include <string>
#include <vector>
#include <sqlite3.h>
struct student {
std::string name;
int age;
int marks;
};
class database {
public:
database(const std::string& filename);
~database();
void add_student(const student& student);
void view_students();
void update_student(int id, const student& new_student);
void delete_student(int id);
private:
sqlite3* db;
char* errormsg;
};
next, let's implement the constructor and destructor of the database class:
code Copy code
database::database(const std::string& filename) {
if (sqlite3_open(filename.c_str(), &db)) {
std::cerr << "can't open database: " << sqlite3_errmsg(db) << std::endl;
exit(1);
}
const char* sql = "create table if not exists students (" \
"id integer primary key," \
"name text," \
"age integer," \
"marks integer);";
if (sqlite3_exec(db, sql, nullptr, nullptr, &errormsg)) {
std::cerr << "sql error: " << errormsg << std::endl;
sqlite3_free(errormsg);
}
}
database::~database() {
sqlite3_close(db);
}
now, let's implement the add_student, view_students, update_student, and delete_student methods:
code Copy code
void database::add_student(const student& student) {
std::stringstream sql;
sql << "insert into students (name, age, marks) values (" \
<< "'" << student.name << "'," \
<< student.age << "," \
<< student.marks << ");";
if (sqlite3_exec(db, sql.str().c_str(), nullptr, nullptr, &errormsg)) {
std::cerr << "sql error: " << errormsg << std::endl;
sqlite3_free(errormsg);
}
}
void database::view_students() {
const char* sql = "select * from students;";
if (sqlite3_exec(db, sql, nullptr, nullptr, &errormsg)) {
std::cerr << "sql error: " << errormsg << std::endl;
sqlite3_free(errormsg);
}
}
void database::update_student(int id, const student& new_student) {
std::stringstream sql;
sql << "update students set name='" << new_student.name << "'," \
<< "age=" << new_student.age << "," \
<< "marks=" << new_student.marks << " where id=" << id << ";";
if (sqlite3_exec(db, sql.str().c_str(), nullptr, nullptr, &errormsg)) {
std::cerr << "sql error: " << errormsg << std::endl;
sqlite3_free(errormsg);
}
}
void database::delete_student(int id) {
std::stringstream sql;
sql << "delete from students where id=" << id << ";";
if (sqlite3_exec(db,