123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
-
- #include <QCoreApplication>
- #include <QDebug>
- #include <QSqlDatabase>
- #include <QSqlDriver>
- #include <QSqlError>
- #include <QSqlQuery>
- #include "calc.h"
- #include "dbpilots.h"
- #include "dbaircraft.h"
- #include <chrono>
- #include <QRandomGenerator>
- #include <QStandardPaths>
- class db
- {
- public:
- static void connect()
- {
- const QString DRIVER("QSQLITE");
- if(QSqlDatabase::isDriverAvailable(DRIVER))
- {
- QSqlDatabase db = QSqlDatabase::addDatabase(DRIVER);
-
-
-
- db.setDatabaseName("logbook.db");
- if(!db.open())
- qWarning() << "MainWindow::DatabaseConnect - ERROR: " << db.lastError().text();
- }
- else
- qWarning() << "MainWindow::DatabaseConnect - ERROR: no driver " << DRIVER << " available";
- }
- static void initexample()
- {
- QSqlQuery query("CREATE TABLE flights (id INTEGER PRIMARY KEY, date NUMERIC)");
- if(!query.isActive())
- qWarning() << "MainWindow::DatabaseInit - ERROR: " << query.lastError().text();
- }
- static void queryexamplenamedbinding()
- {
- QSqlQuery query;
-
-
- query.prepare("SELECT * from people WHERE name LIKE :name");
- query.bindValue(":name", "%Linus%");
- query.bindValue(":id",2);
- query.exec();
-
- if(query.first());
- else
- qDebug() << ("No entry found");
- query.previous();
- while (query.next()) {
- QString name = query.value(1).toString();
- int id = query.value(0).toInt();
- qDebug() << name << id;
- }
-
- }
-
- static QVector<QString> SelectFlightDate(QString doft)
- {
- QSqlQuery query;
- if (doft == "ALL")
- {
- query.prepare("SELECT * FROM flights ORDER BY doft DESC, tofb ASC");
- qDebug() << "All flights selected";
- }else
- {
- query.prepare("SELECT * FROM flights WHERE doft = ? ORDER BY tofb ASC");
- query.addBindValue(doft);
- qDebug() << "Searching flights for " << doft;
- }
- query.exec();
- if(query.first());
- else
- {
- qDebug() << ("No flight with this date found");
- QVector<QString> flight;
- return flight;
- }
- query.previous();
- query.last();
- int numRows = query.at() + 1;
- query.first();
- query.previous();
- QVector<QString> flight(numRows * 9);
- int index = 0;
- while (query.next()) {
- QString id = query.value(0).toString();
- QString doft = query.value(1).toString();
- QString dept = query.value(2).toString();
- QString tofb = calc::minutes_to_string((query.value(3).toString()));
- QString dest = query.value(4).toString();
- QString tonb = calc::minutes_to_string((query.value(5).toString()));
- QString tblk = calc::minutes_to_string((query.value(6).toString()));
- QString pic = dbPilots::retreivePilotNameFromID(query.value(7).toString());
- QString acft = dbAircraft::retreiveRegistration(query.value(8).toString());
-
- flight[index] = id;
- ++index;
- flight[index] = doft;
- ++index;
- flight[index] = dept;
- ++index;
- flight[index] = tofb;
- ++index;
- flight[index] = dest;
- ++index;
- flight[index] = tonb;
- ++index;
- flight[index] = tblk;
- ++index;
- flight[index] = pic;
- ++index;
- flight[index] = acft;
- ++index;
- }
- return flight;
- }
- };
|