PDA

View Full Version : Database updating problem



ayanda83
23rd February 2014, 18:58
Hi there everyone. I'm writing an app that connects to an SQLite database. The connection to the database is fine but the problem come when I try updating the same database from a different class. The database does not update. Can somebody please tell me how one can reference one database from different classes in the program. Do I need to open the database every time I want to reference the database. here is my code.
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("Study Planner");
JobsAtHandModel = new QStringListModel(this);

dataBase = QSqlDatabase::addDatabase("QSQLITE");
dataBase.setDatabaseName("C:/My_Databases/Unisa_Database/UnisaDB.db");
dataBase.open(); //This works

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{


}

void MainWindow::checkJobsAtHand()
{
/*QString sqlStr(QString("SELECT Name, Surname, Address, ContactNo FROM ClientTable")), tempStr;
QSqlQuery qry;
qry.prepare(sqlStr);

if(qry.exec())
{
while(qry.next())
{
tempStr = QString("%1 %2 | %3 | %4").arg(qry.value(0).toString()).arg(qry.value(1).to String())
.arg(qry.value(2).toString()).arg(qry.value(3).toS tring());
job.append(tempStr);

}
}
JobsAtHandModel->setStringList(job);
ui->lcdNumber->display(job.count());*/
}

void MainWindow::checkJobsCompleted()
{

}

void MainWindow::on_AddCourseBtn_clicked()
{
NewModule* newMod = new NewModule(this);
newMod->setModal(true);
newMod->exec();
}



DIFFERENT CLASS


#include "newmodule.h"
#include "ui_newmodule.h"

NewModule::NewModule(QWidget *parent) :
QDialog(parent),
ui(new Ui::NewModule)
{
ui->setupUi(this);
}

NewModule::~NewModule()
{
delete ui;
}

void NewModule::on_pushButton_clicked()
{

}

void NewModule::on_SubmitBtn_clicked()
{
QSqlDatabase dataBase = QSqlDatabase::addDatabase("QSQLITE");
dataBase.setDatabaseName("C:/My_Databases/Unisa_Database/UnisaDB.db");
dataBase.open();

QString sqlStr(QString("INSERT INTO Modules VALUES(%1, %2, %3, %4").arg(ui->moduleCodeLE->text())
.arg(ui->moduleNameLE->text()).arg(ui->LectureLE->text()).arg(ui->contactNo->text()));
QSqlQuery qry;
qry.prepare(sqlStr);
qry.exec(); //This doesn't work
}

stampede
23rd February 2014, 19:36
QSqlDatabase objects can be copied, you can pass the object to the "NewModule" instance from MainWindow.
Another thing, if you create another database with the same connection name (default one in your case) then previous connection is removed, I don't think this is what you want.
So instead of


void NewModule::on_SubmitBtn_clicked()
{
QSqlDatabase dataBase = QSqlDatabase::addDatabase("QSQLITE");
dataBase.setDatabaseName("C:/My_Databases/Unisa_Database/UnisaDB.db");
dataBase.open();
...

try


void NewModule::on_SubmitBtn_clicked()
{
QSqlDatabase dataBase = QSqlDatabase::database();
...

Lesiok
24th February 2014, 08:54
What does "does not work" ? What is a result from qry.lastError() and dataBase.lastError() ?

ayanda83
26th February 2014, 18:22
My problem here is that the database object (i.e. QSqlDatabase) is declare in the scope of the class MainWindow which means the database object is inaccessible in the scope of the class NewModule. The issue here is that the sole purpose of the class NewModule is to add a new record in the database which happens to have been declared in the scope of the class MainWindow. That is my predicament. any suggestions will be appreciated.

stampede
26th February 2014, 19:00
any suggestions will be appreciated.
Suggestion is - pass the object down to the child class or use QSqlDatabase::database() method to get access to the previously created connection.