When I click on Show Table Button then data should display in Table (TableData.qml) . But Data is not showing in Table .It is showing empty
-
dbmanager.h #ifndef DBMANAGER_H #define DBMANAGER_H #include <QObject> #include <QSqlDatabase> class DbManager : public QObject { Q_OBJECT public: explicit DbManager(QObject *parent = nullptr); DbManager(const QString& path); bool isOpen() const; bool createTable(); bool addRecord(const QString &name, const QString &fatherName,const QString &motherName); void printAllRecords() const; QList<QVariantMap> execQuery(const QString& query); public slots: Q_INVOKABLE bool saveToSqlite(const QString &data); private: QSqlDatabase m_db; }; #endif // DBMANAGER_H dbmanager.cpp #include "dbmanager.h" #include <QFile> #include <QTextStream> #include <QDateTime> #include <QDebug> #include <QSqlQuery> #include <QSqlError> #include <QSqlRecord> DbManager::DbManager(QObject *parent) : QObject(parent){} DbManager::DbManager(const QString &path) { m_db = QSqlDatabase::addDatabase("QSQLITE"); m_db.setDatabaseName(path); if (!m_db.open()) { qDebug() << "Error: connection with database fail"; } else { qDebug() << "Database Connected Successfully !!!"; } createTable(); } bool DbManager::isOpen() const { return m_db.isOpen(); } bool DbManager::createTable() { bool success = false; QSqlQuery query; query.prepare("CREATE TABLE student (id INTEGER PRIMARY KEY, name TEXT, fatherName TEXT, motherName TEXT)"); if (!query.exec()) { qDebug() << "Couldn't create the table 'student': one might already exist."; success = false; } return success; } bool DbManager::addRecord(const QString &name, const QString &fatherName, const QString &motherName) { if (isOpen()) { QSqlQuery queryAdd; queryAdd.prepare("INSERT INTO student (name, fatherName, motherName) " "VALUES (:name, :fatherName, :motherName)"); queryAdd.bindValue(":name", name); queryAdd.bindValue(":fatherName", fatherName); queryAdd.bindValue(":motherName", motherName); if (queryAdd.exec()) { return true; } else { qDebug() << "Data insertion failed: " << queryAdd.lastError(); } } else { qDebug() << "Database is not open!"; } return false; } void DbManager::printAllRecords() const { qDebug() << "Persons in Database:"; QSqlQuery query("SELECT * FROM student"); if (!query.exec()) { qDebug() << "Error executing query: " << query.lastError().text(); return; } while (query.next()) { QString name = query.value("name").toString(); QString fatherName = query.value("fatherName").toString(); QString motherName = query.value("motherName").toString(); qDebug() << "Name: " << name; qDebug() << "Father's Name: " << fatherName; qDebug() << "Mother's Name: " << motherName; } } bool DbManager::saveToSqlite(const QString &data) { QStringList list =data.split(","); qDebug()<<"Input Data" <<list; addRecord(list.at(0),list.at(1),list.at(2)); printAllRecords(); } QList<QVariantMap> DbManager::execQuery(const QString& query) { QList<QVariantMap> results; if (isOpen()) { QSqlQuery sqlQuery(query); if (sqlQuery.exec()) { while (sqlQuery.next()) { QVariantMap row; row["name"] = sqlQuery.value("name").toString(); row["fatherName"] = sqlQuery.value("fatherName").toString(); row["motherName"] = sqlQuery.value("motherName").toString(); results.append(row); } } else { qDebug() << "Query execution failed: " << sqlQuery.lastError(); } } else { qDebug() << "Database is not open!"; } return results; } main.cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include <dbmanager.h> static const QString path = "example1.db"; int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; DbManager dbManager(path); // engine.rootContext()->setContextProperty("d_bmanager", &dbManager); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); } main.qml import QtQuick 2.15 import QtQuick.Window 2.15 import QtQuick.Controls 2.12 import QtQuick.Layouts 1.3 import QtQuick.Dialogs 1.2 Window { id: window width: 500 height: 400 visible: true title: qsTr("Student Login Form") property string name: "" property string fatherName: "" property string motherName: "" Rectangle{ Label { x: 50 y: 25 text: qsTr("Name") } Label { x: 50 y: 65 text: qsTr("Father's Name") rotation: -1.907 } Label { x: 50 y: 110 text: qsTr("Mother's Name") } TextField { id: nameTextField x: 175 y: 25 width: 238 height: 29 placeholderText: qsTr("Enter your name") onTextChanged: name=text } TextField { id: fatherNameTextField x: 175 y: 65 width: 238 height: 29 placeholderText: qsTr("Enter your Father's name ") onTextChanged: fatherName=text } TextField { id: motherNameTextField x: 175 y: 105 width: 238 height: 29 placeholderText: qsTr("Enter your Mother's name ") onTextChanged: motherName=text } } Button { id: button x: 220 y: 200 width: 99 height: 37 text: qsTr("Submit") onClicked: { var csv_data = nameTextField.text + "," + fatherNameTextField.text +","+ motherNameTextField.text; if (d_bmanager.saveToSqlite(csv_data)){ console.log("Data saved to Sqlite successfully !!!"); } else{ console.error("Error While Saving Data to Sqlite."); } } } StackView{ id:stackView anchors.fill: parent } Button { id: button2 x: 350 y: 200 width: 99 height: 37 text: qsTr("Show Table") onClicked: { stackView.push(secondPage) } Component{ id:secondPage TableData{} } } } TableData.qml import QtQuick 2.7 import QtQuick.Window 2.0 import QtQuick.Controls 1.4 Window { width: 700 height: 500 visible: true Component.onCompleted: { var query = "SELECT * FROM student"; var result = d_bmanager.execQuery(query); for (var i = 0; i < result.length; i++) { myModel.append({ name: result[i].name, fatherName: result[i].fatherName, motherName: result[i].motherName }); } } TableView { id: myTable anchors.fill: parent anchors.margins: 5 TableViewColumn { role: name title: "Name" width: 250 horizontalAlignment: Text.AlignHCenter } TableViewColumn { role: fatherName title: "Father's Name" width: 250 horizontalAlignment: Text.AlignHCenter } TableViewColumn { role: motherName title: "Mother's Name" width: 250 horizontalAlignment: Text.AlignHCenter } } } -
Hi,
Please stop posting the same stuff over and over again and ignoring afterwards.
You already have gotten replies to several of your threads that you did not acknowledge nor replied to. This is slowly but surely getting irritating not to say insulting to the people trying to help. -
Hi,
Please stop posting the same stuff over and over again and ignoring afterwards.
You already have gotten replies to several of your threads that you did not acknowledge nor replied to. This is slowly but surely getting irritating not to say insulting to the people trying to help.@SGaist HI I am not able to get output so I sent whole code. So that anyone can understand my whole code and reply
-
@SGaist HI I am not able to get output so I sent whole code. So that anyone can understand my whole code and reply
@Pappu-Kumar-Keshari it's your job to reduce your code to the strict minimum. Don't put your whole GUI when one of the core element does not work. Don't put your whole database handler class, just the functions that are currently required. That's all, simplify your code.
By the way, don't keep an instance of QSqlDatabase as member variable. That's not how that class shall be used and you can find why in its documentation.
-
@Pappu-Kumar-Keshari it's your job to reduce your code to the strict minimum. Don't put your whole GUI when one of the core element does not work. Don't put your whole database handler class, just the functions that are currently required. That's all, simplify your code.
By the way, don't keep an instance of QSqlDatabase as member variable. That's not how that class shall be used and you can find why in its documentation.
@SGaist Hi, Can you please resolve my issue ? I am unable to fetch record from Sqlite to Table in QML. Which time you will be free ? I will share my screen on Zoom then you will get clear idea about my project. It's my humble request to you.
-
@SGaist Hi, Can you please resolve my issue ? I am unable to fetch record from Sqlite to Table in QML. Which time you will be free ? I will share my screen on Zoom then you will get clear idea about my project. It's my humble request to you.
@Pappu-Kumar-Keshari Did you actually address what @SGaist told you?
Where is simplified code?
Did you remove QSqlDatabase member variable?
Or do you simply want that somebody fixes your code for free?