QList<Site*> SitesDatabaseInterface::getAllSites() { qDebug() << "db: " << this->dbPath; QList<Site*> siteList; QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(this->dbPath); if(false == db.open()) { qDebug() << "can not open database"; } QSqlQuery query= db.exec("SELECT * FROM sites"); while (query.next()) { int id = query.value(0).toInt(); QString nom = query.value(1).toString(); QString years = query.value(2).toString(); QStringList yearsList = years.split(";"); Site* site = new Site(); site->setId(id); site->setNom(nom); site->setYears(yearsList); siteList.append(site); } db.close(); return siteList; }
Site* SitesDatabaseInterface::getSite(int id) { Site* site = new Site(); qDebug() << "db: " << this->dbPath; QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(this->dbPath); if(false == db.open()) { qDebug() << "can not open database"; } QSqlQuery query; query.prepare("SELECT * FROM sites WHERE ID = :siteId "); query.bindValue(":siteId", id); if(query.exec()) { query.next(); int id = query.value(0).toInt(); QString nom = query.value(1).toString(); QString years = query.value(2).toString(); QStringList yearsList = years.split(";"); site->setId( id ); site->setNom( nom ); site->setYears( yearsList ); } else { qDebug() << "SQL ERROR 0: " << query.lastError(); } db.close(); return site; }