#include "dbmanager.h"
DbManager
::DbManager(const QString &path
) { Q_UNUSED(path);
m_db.setDatabaseName("C:/Users/ss/Desktop/TestData.db");
if (!m_db.open())
{
qDebug() << "Error: connection with database failed";
}
else
{
qDebug() << "Database: connection ok";
}
}
bool DbManager
::addEntry(const QString &name
) {
bool success = false;
query.prepare("INSERT INTO TestData VALUES (:time)");
query.bindValue(":time, time);
if(query.exec())
{
success = true;
}
else
{
qDebug() << "addtemperature error:"
<< query.lastError();
}
return success;
qDebug() << query.isValid();
QSqlQueryModel model;
model.setQuery("SELECT * FROM TestData");
for (int i = 0; i < model.rowCount(); ++i) {
int id = model.record(i).value("id").toInt();
QString name = model.record(i).value("time").toString();
qDebug() << id << time;
}
QLabel *labelExp = new QLabel(this);
labelExp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
labelExp->setText("first line\nsecond line");
labelExp->setAlignment(Qt::AlignBottom | Qt::AlignRight);
//Named binding
QSqlQuery query1;
query1.prepare("INSERT INTO TestData (time,temperature) VALUES (:time, "
":temperature)");
query1.bindValue(":time","2020-11-31 10:19:38");
query1.bindValue(":temperature", 27.4);
query1.exec();
//Positional Binding
QSqlQuery query2;
query2.prepare("INSERT INTO TestData (time,temperature) VALUES (?, ?, )");
query2.addBindValue("2010-06-31 12:08:35");
query2.addBindValue(30.6);
query2.exec();
QSqlQueryModel *model1 = new QSqlQueryModel;
model1->setQuery("SELECT time, temperature FROM TestData");
model1->setHeaderData(0, Qt::Horizontal, tr("Time"));
model1->setHeaderData(1, Qt::Horizontal, tr("Temperature"));
QTableView *view = new QTableView;
view->setModel(model1);
view->show();
model1->setHeaderData(0, Qt::Horizontal, QObject::tr("Time"));
model1->setHeaderData(1, Qt::Horizontal, QObject::tr("Temperature"));
}
#include "dbmanager.h"
DbManager::DbManager(const QString &path)
{ Q_UNUSED(path);
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName("C:/Users/ss/Desktop/TestData.db");
if (!m_db.open())
{
qDebug() << "Error: connection with database failed";
}
else
{
qDebug() << "Database: connection ok";
}
}
bool DbManager::addEntry(const QString &name)
{
bool success = false;
QSqlQuery query;
query.prepare("INSERT INTO TestData VALUES (:time)");
query.bindValue(":time, time);
if(query.exec())
{
success = true;
}
else
{
qDebug() << "addtemperature error:"
<< query.lastError();
}
return success;
qDebug() << query.isValid();
QSqlQueryModel model;
model.setQuery("SELECT * FROM TestData");
for (int i = 0; i < model.rowCount(); ++i) {
int id = model.record(i).value("id").toInt();
QString name = model.record(i).value("time").toString();
qDebug() << id << time;
}
QLabel *labelExp = new QLabel(this);
labelExp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
labelExp->setText("first line\nsecond line");
labelExp->setAlignment(Qt::AlignBottom | Qt::AlignRight);
//Named binding
QSqlQuery query1;
query1.prepare("INSERT INTO TestData (time,temperature) VALUES (:time, "
":temperature)");
query1.bindValue(":time","2020-11-31 10:19:38");
query1.bindValue(":temperature", 27.4);
query1.exec();
//Positional Binding
QSqlQuery query2;
query2.prepare("INSERT INTO TestData (time,temperature) VALUES (?, ?, )");
query2.addBindValue("2010-06-31 12:08:35");
query2.addBindValue(30.6);
query2.exec();
QSqlQueryModel *model1 = new QSqlQueryModel;
model1->setQuery("SELECT time, temperature FROM TestData");
model1->setHeaderData(0, Qt::Horizontal, tr("Time"));
model1->setHeaderData(1, Qt::Horizontal, tr("Temperature"));
QTableView *view = new QTableView;
view->setModel(model1);
view->show();
model1->setHeaderData(0, Qt::Horizontal, QObject::tr("Time"));
model1->setHeaderData(1, Qt::Horizontal, QObject::tr("Temperature"));
}
To copy to clipboard, switch view to plain text mode
Bookmarks