Answer the question
In order to leave comments, you need to log in
Carefully! DB not working! Something went wrong?
The database connection code is provided below.
EventsForDate (const QDate &date) function stolen from QT example (Calendar)
The only thing that is in the table (Grocery shopping) does not appear when the program starts :( The
database is located in the project folder, here is a screenshot:
main.cpp
#include <QtQml>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "qtquickcontrolsapplication.h"
#include "sqleventmodel.h"
#include "listmodel.h"
int main(int argc, char *argv[])
{
QtQuickControlsApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterType<SqlEventModel>("project.calendar", 1, 0, "SqlEventModel");
SqlEventModel database;
database.createConnection();
engine.rootContext()->setContextProperty("database", &database);
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
#ifndef SQLEVENTMODEL_H
#define SQLEVENTMODEL_H
#include <QList>
#include <QObject>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlDatabase>
#include <QFile>
#include <QDate>
#include "event.h"
#define DATABASE_HOSTNAME "EventsDB"
#define DATABASE_NAME "events.db"
#define TABLE "Events" // Название таблицы
#define TABLE_NAME "name" // Вторая колонка
#define TABLE_SD "startDate" // Третья колонка
#define TABLE_HOUR "hour" // Четвертая колонка
#define TABLE_ED "endDate"
#define TABLE_MIN "min"
class SqlEventModel : public QObject
{
Q_OBJECT
public:
explicit SqlEventModel(QObject *parent = 0);
~SqlEventModel();
void createConnection(); //открытие БД
Q_INVOKABLE QList<QObject*> eventsForDate(const QDate &date);
private:
QSqlDatabase db;
bool createTable();
void closeDataBase();
};
#include "sqleventmodel.h"
#include <QDebug>
#include <QFileInfo>
#include <QSqlError>
#include <QSqlQuery>
SqlEventModel::SqlEventModel(QObject *parent) : QObject(parent)
{
createConnection();
}
SqlEventModel::~SqlEventModel()
{
}
void SqlEventModel::createConnection() //открытие БД
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setHostName(DATABASE_HOSTNAME);
db.setDatabaseName(DATABASE_NAME);
if (!db.open()) {
qFatal("Cannot open database");
return;
}
return;
}
void SqlEventModel::closeDataBase()
{
db.close();
}
QList<QObject*> SqlEventModel::eventsForDate(const QDate &date)
{
const QString queryStr = QString::fromLatin1("SELECT * FROM " TABLE " WHERE '%1' >= " TABLE_SD " AND '%1' <= " TABLE_ED "").arg(date.toString("yyyy-MM-dd"));
QSqlQuery query(queryStr);
QList<QObject*> events;
while (query.next()) {
Event *event = new Event(this);
event->setName(query.value(TABLE_NAME).toString());
QDateTime startDate;
startDate.setDate(query.value(TABLE_SD).toDate());
startDate.setTime(QTime(0, 0).addSecs(query.value(TABLE_HOUR).toInt()));
event->setStartDate(startDate);
QDateTime endDate;
endDate.setDate(query.value(TABLE_ED).toDate());
endDate.setTime(QTime(0, 0).addSecs(query.value(TABLE_MIN).toInt()));
event->setEndDate(endDate);
events.append(event);
}
return events;
}
bool SqlEventModel::createTable()
{
QSqlQuery query;
if(!query.exec( "CREATE TABLE " TABLE " ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
TABLE_NAME " VARCHAR(255) NOT NULL,"
TABLE_SD " VARCHAR(255) NOT NULL,"
TABLE_HOUR " INTEGER NOT NULL,"
TABLE_ED " VARCHAR(255) NOT NULL,"
TABLE_MIN " INTEGER NOT NULL"
" )"
)){
qDebug() << "DataBase: error of create " << TABLE;
qDebug() << query.lastError().text();
return false;
} else {
return true;
}
return false;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question