bool ScientistRepository::addScientist(Scientist scientist) { db.open(); if (!db.isOpen()) { return false; } QSqlQuery query(db); stringstream sqlQuery; sqlQuery << "INSERT INTO Scientists (name, sex, yearBorn, yearDied, info) VALUES (" << "'" << scientist.getName() << "', " << scientist.getSex() << ", " << scientist.getYearBorn() << ", " << scientist.getYearDied() << ", " << "'" << scientist.getInfo() << "'" << ")"; if (!query.exec(QString::fromStdString(sqlQuery.str()))) { return false; } db.close(); return true; }
bool ScientistRepository::editScientist(Scientist scientist, int id) { db.open(); if (!db.isOpen()) { return false; } QSqlQuery query(db); stringstream sqlQuery; sqlQuery << "UPDATE Scientists SET " << "name = " << "'" << scientist.getName() << "', " << "sex = " << scientist.getSex() << ", " << "yearBorn = " << scientist.getYearBorn() << ", " << "yearDied = " << scientist.getYearDied() << ", " << "info = " << "'" << scientist.getInfo() << "' " << "WHERE id = " << id; if (!query.exec(QString::fromStdString(sqlQuery.str()))) { return false; } db.close(); return true; }