56 lines
1.6 KiB
C++
56 lines
1.6 KiB
C++
#include "sqlprofilemodel.h"
|
|
|
|
static void createTable()
|
|
{
|
|
if (QSqlDatabase::database().tables().contains(QStringLiteral("Contacts"))) {
|
|
// The table already exists; we don't need to do anything.
|
|
return;
|
|
}
|
|
|
|
QSqlQuery query;
|
|
|
|
//creat eth etable to store the profiles
|
|
if (!query.exec(
|
|
"CREATE TABLE IF NOT EXISTS `profiles` ( "
|
|
" `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,"
|
|
" `name` TEXT NOT NULL "
|
|
" );")) {
|
|
qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
|
|
}
|
|
|
|
//create the table to store the times
|
|
if (!query.exec(
|
|
"CREATE TABLE IF NOT EXISTS `times` ("
|
|
" `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,"
|
|
" `profileid` INTEGER NOT NULL,"
|
|
" `time` INTEGER NOT NULL"
|
|
" );")) {
|
|
qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
|
|
}
|
|
}
|
|
|
|
SqlProfileModel::SqlProfileModel(QObject *parent) : QSqlTableModel(parent)
|
|
{
|
|
qDebug("ProfileModel constructor");
|
|
createTable();
|
|
setTable("profiles");
|
|
select();
|
|
}
|
|
|
|
QVariant SqlProfileModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (role < Qt::UserRole)
|
|
return QSqlTableModel::data(index, role);
|
|
|
|
const QSqlRecord sqlRecord = record(index.row());
|
|
return sqlRecord.value(role - Qt::UserRole);
|
|
}
|
|
|
|
QHash<int, QByteArray> SqlProfileModel::roleNames() const
|
|
{
|
|
QHash<int, QByteArray> names;
|
|
names[Qt::UserRole + 0] = "id";
|
|
names[Qt::UserRole + 1] = "name";
|
|
|
|
return names;
|
|
}
|