Solved Connecting to Database in Qt
-
@Swati777999 said in Connecting to Database in Qt:
what to use whether SQL Model Class or QTableview?
Both.
Please see how it is done here: https://doc.qt.io/qt-5/qsqltablemodel.html -
@jsulm said in Connecting to Database in Qt:
@Swati777999 said in Connecting to Database in Qt:
what to use whether SQL Model Class or QTableview?
Both.
Please see how it is done here: https://doc.qt.io/qt-5/qsqltablemodel.htmlI used this strategy but I am not getting anything in my main window. :(
-
@Swati777999 Then you are doing something wrong and should provide more information to get meaningful answer.
Best would be related code. -
#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"));
}
-
@Swati777999 said in Connecting to Database in Qt:
QSqlQueryModel model;
Your model is a local variable and is destroyed as soon as it goes out of scope.
And please format you code properly. -
For whatever reason there are two models...
QSqlQueryModel model
andQSqlQueryModel* model1
-
@jsulm I didn't get your point clearly and how to format the code; do you mean looking after the indentation/ improving the readability of the codes?
-
@Pl45m4 I was trying to see how QSqlQuery and QSqlQueryModel are different from each other, no other reason apart from that.
-
@Swati777999 said in Connecting to Database in Qt:
I didn't get your point clearly and how to format the code; do you mean looking after the indentation/ improving the readability of the codes?
When pasting code please select it all and use the the </> Code button:
so it comes out like this with ``` above & below it in your post
As for your code.
addEntry()
at least does little --- most of the code is not executed --- since you have an unconditionalreturn
statement before most of the body. If people are supposed to look at this for you, you should really clear it up a lot before posting. -
@Swati777999 said in Connecting to Database in Qt:
I didn't get your point clearly
We are talking about absolute C++ basics now! Please learn those!
bool DbManager::addEntry(const QString &name) { ... QSqlQueryModel model; ... }
In the code above "model" only exists inside DbManager::addEntry. As soon as DbManager::addEntry is done "mode" goes out of scope and is deleted!
Also, I see that you do "return success;" before you create your model - how can this work at all?!