add table creation and first table model.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
set(vehiclevoyage_SRCS
|
||||
db/sqlcar.cpp
|
||||
db/sqlvehicle.cpp
|
||||
db/sqlservicerecord.cpp
|
||||
main.cpp
|
||||
)
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 <Kevin Whitaker> <eyecreate@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sqlcar.h"
|
||||
56
src/db/sqlvehicle.cpp
Normal file
56
src/db/sqlvehicle.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) 2020 <Kevin Whitaker> <eyecreate@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sqlvehicle.h"
|
||||
#include <QSqlRecord>
|
||||
|
||||
SqlVehicle::SqlVehicle(QObject* parent)
|
||||
{
|
||||
setTable("vehicles");
|
||||
}
|
||||
|
||||
|
||||
QHash<int, QByteArray> SqlVehicle::roleNames() const
|
||||
{
|
||||
QHash<int, QByteArray> roles;
|
||||
// record() returns an empty QSqlRecord
|
||||
for (int i = 0; i < this->record().count(); i ++) {
|
||||
roles.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
|
||||
}
|
||||
return roles;
|
||||
}
|
||||
|
||||
|
||||
QVariant SqlVehicle::data(const QModelIndex& index, int role) const
|
||||
{
|
||||
QVariant value = QSqlQueryModel::data(index, role);
|
||||
if(role < Qt::UserRole)
|
||||
{
|
||||
value = QSqlQueryModel::data(index, role);
|
||||
}
|
||||
else
|
||||
{
|
||||
int columnIdx = role - Qt::UserRole - 1;
|
||||
QModelIndex modelIndex = this->index(index.row(), columnIdx);
|
||||
value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -15,18 +15,24 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef SQLCAR_H
|
||||
#define SQLCAR_H
|
||||
#ifndef SQLVEHICLE_H
|
||||
#define SQLVEHICLE_H
|
||||
|
||||
#include <QSqlQueryModel>
|
||||
#include <QSqlTableModel>
|
||||
|
||||
/**
|
||||
* @todo write docs
|
||||
* Model to access list of vehicles
|
||||
*/
|
||||
class SqlCar : public QSqlQueryModel
|
||||
class SqlVehicle : public QSqlTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit SqlVehicle(QObject *parent);
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
QVariant data(const QModelIndex &index, int role) const;
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif // SQLCAR_H
|
||||
#endif // SQLVEHICLE_H
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlError>
|
||||
#include <QDebug>
|
||||
#include <QSqlQuery>
|
||||
|
||||
Q_DECL_EXPORT int main(int argc, char *argv[])
|
||||
{
|
||||
@@ -51,6 +52,12 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
|
||||
if(!QFile(db.databaseName()).exists())
|
||||
{
|
||||
QFile(db.databaseName()).open(QIODevice::ReadWrite);
|
||||
//Create table structure
|
||||
db.open();
|
||||
db.exec("CREATE TABLE vehicles (id INTEGER PRIMARY KEY, name TEXT, maker TEXT, model TEXT, year INTEGER, image TEXT);");
|
||||
db.exec("CREATE TABLE records (id INTEGER PRIMARY KEY, servicetype TEXT, date INTEGER, miles INTEGER, notes TEXT);");
|
||||
db.commit();
|
||||
db.close();
|
||||
}
|
||||
if(!db.open())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user