jon-ecm
6th May 2009, 02:06
Please Help!
I've been fighting this for a long time and would be more than greatfull someone could help...Basicly I'v got a sql table (a QTableView and a QSqlTableModel) I'v got everything working ***Except*** One thing!
I can't find any way to set the data of a field...Here's what I'm trying to do: I'v got a table with a buch of columns and rows and a text box where the user enters the tag and it appends the current date time and adds 1 to the daysAttended column
Here's the function that is executed when the user hit's enter in the textBox
void MainWindow::locate()
{
if(db.isOpen())
{
QString datesAttended = "";
int daysAttended = 0;
int daysMissed = 0;
int matchingRow = 0;
QString searchTag = barcodeEdit->text().toUpper();
if(query->exec(QString("select * from persons where UID = \"%0\" LIMIT 0,1").arg(searchTag)))
{
if(query->isSelect() && query->next() && query->value(0).isValid())
{
int matchingRow = query->value(0).toInt();
daysAttended = model->record(matchingRow).value(6).toInt();
datesAttended = model->record(matchingRow).value(5).toString();
if(model->setData(model->index(matchingRow,6),QVariant(daysAttended + 1)) && model->setData(model->index(matchingRow,5),QVariant(datesAttended + QDateTime::currentDateTime().toString())))
{barcodeEdit->setText("");} else {
QMessageBox::critical(0, qApp->tr("Error Setting Record!"),
qApp->tr(" %0.\n \n"
"Click Close to continue.").arg(model->lastError().text()), QMessageBox::Close);}
} else {
QMessageBox::critical(0, qApp->tr("Could not find an entry with the matching criticia!"),
qApp->tr("Unable to find a person with the matching criticia! \n\n"
"Click Close to continue."), QMessageBox::Close);}
} else {
QMessageBox::critical(0, qApp->tr("Error Executing Query!"),
qApp->tr(" %0.\n \n"
"Click Close to continue.").arg(query->lastError().text()), QMessageBox::Close);}
} else {
QMessageBox::critical(0, qApp->tr("Database Not Open!"),
qApp->tr("You must have a database open before you can use this function!. \n\n"
"Click Close to continue."), QMessageBox::Close);}
}
That works and it dose what I want accept on thing...after that saving dosn't work! :confused: when I save the changes it returns the following error:
database is locked Unable to fetch row and it dosn't save!
Here's my save function
bool MainWindow::save()
{
if(db.isOpen()){
model->database().transaction();
if (model->submitAll()) {
model->database().commit();
return true;
} else {
model->database().rollback();
QMessageBox::warning(this, tr("Cached Table"),
tr("The database reported an error: %1")
.arg(model->lastError().text()));
return false;
}
} else {
QMessageBox::warning(this, tr("Database Not Open!"),
tr("No database is open!"));
return false;
}
}
I've been fighting this for a long time and would be more than greatfull someone could help...Basicly I'v got a sql table (a QTableView and a QSqlTableModel) I'v got everything working ***Except*** One thing!
I can't find any way to set the data of a field...Here's what I'm trying to do: I'v got a table with a buch of columns and rows and a text box where the user enters the tag and it appends the current date time and adds 1 to the daysAttended column
Here's the function that is executed when the user hit's enter in the textBox
void MainWindow::locate()
{
if(db.isOpen())
{
QString datesAttended = "";
int daysAttended = 0;
int daysMissed = 0;
int matchingRow = 0;
QString searchTag = barcodeEdit->text().toUpper();
if(query->exec(QString("select * from persons where UID = \"%0\" LIMIT 0,1").arg(searchTag)))
{
if(query->isSelect() && query->next() && query->value(0).isValid())
{
int matchingRow = query->value(0).toInt();
daysAttended = model->record(matchingRow).value(6).toInt();
datesAttended = model->record(matchingRow).value(5).toString();
if(model->setData(model->index(matchingRow,6),QVariant(daysAttended + 1)) && model->setData(model->index(matchingRow,5),QVariant(datesAttended + QDateTime::currentDateTime().toString())))
{barcodeEdit->setText("");} else {
QMessageBox::critical(0, qApp->tr("Error Setting Record!"),
qApp->tr(" %0.\n \n"
"Click Close to continue.").arg(model->lastError().text()), QMessageBox::Close);}
} else {
QMessageBox::critical(0, qApp->tr("Could not find an entry with the matching criticia!"),
qApp->tr("Unable to find a person with the matching criticia! \n\n"
"Click Close to continue."), QMessageBox::Close);}
} else {
QMessageBox::critical(0, qApp->tr("Error Executing Query!"),
qApp->tr(" %0.\n \n"
"Click Close to continue.").arg(query->lastError().text()), QMessageBox::Close);}
} else {
QMessageBox::critical(0, qApp->tr("Database Not Open!"),
qApp->tr("You must have a database open before you can use this function!. \n\n"
"Click Close to continue."), QMessageBox::Close);}
}
That works and it dose what I want accept on thing...after that saving dosn't work! :confused: when I save the changes it returns the following error:
database is locked Unable to fetch row and it dosn't save!
Here's my save function
bool MainWindow::save()
{
if(db.isOpen()){
model->database().transaction();
if (model->submitAll()) {
model->database().commit();
return true;
} else {
model->database().rollback();
QMessageBox::warning(this, tr("Cached Table"),
tr("The database reported an error: %1")
.arg(model->lastError().text()));
return false;
}
} else {
QMessageBox::warning(this, tr("Database Not Open!"),
tr("No database is open!"));
return false;
}
}