Verified Commit e48051ca authored by Romain De Laage De Bellefaye's avatar Romain De Laage De Bellefaye 🌳
Browse files

First tests sql

parent 0ca8f77f
#include <iostream>
#include "autosql.h"
Database::Database(std::string path): db(QSqlDatabase::addDatabase("QSQLITE")) {
db.setDatabaseName(path.c_str());
if (!db.open()) {
throw "Unable to open database";
}
db.exec("CREATE TABLE IF NOT EXISTS automates (nom VARCHAR(30) PRIMARY KEY, voisinage INTEGER, fonction INTEGER)");
}
std::vector<QString> Database::getAutomates() const {
QSqlQuery query = db.exec("SELECT nom FROM automates");
std::vector<QString> names;
if(query.first()) {
names.push_back(query.value("nom").toString());
}
while(query.next()) {
names.push_back(query.value("nom").toString());
}
return names;
}
int main() {
Database db("test.sqlite");
auto automates = db.getAutomates();
for(auto& automate: automates) {
std::cout << automate.toStdString() << std::endl;
}
return 0;
}
#ifndef _AUTOSQL_H
#define _AUTOSQL_H
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QSqlError>
#include <QSqlQuery>
#include <QVariant>
class Database {
private:
QSqlDatabase db;
public:
Database(std::string path);
~Database() { db.close(); }
std::vector<QString> getAutomates() const;
};
#endif
QT += core
QT += sql
HEADERS += autosql.h
SOURCES += autosql.cpp
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment