PDA

View Full Version : QSqlTableModel setData not working?



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;
}
}

jon-ecm
6th May 2009, 05:09
can't anybody please!!!! help me? I've been sturgling with this thing so much!
Oh and one more thing...setData dose update the view but changes cannot be saved

wysota
6th May 2009, 09:10
The problem is with the database not with setData(). You probably have more than one connection to the same database and you operate on the same table with both of them. Seems that the database is set in a way that prevents such situations. If I were to guess, I'd say this was a MySQL installation.

jon-ecm
6th May 2009, 23:35
It's using SQLITE

wysota
7th May 2009, 00:00
It's using SQLITE

Yeah, I had issues with locks with that one too. You are facing a database issue. Try simplifying your code (especially parts that use SQL queries), maybe you can avoid the locks.

jon-ecm
7th May 2009, 00:37
Ok! Thank you so much!!! :) :D
Also do you have any better idea of searching for the user with the matching tag?
Is there a way to do this without talking directly to the database?

wysota
7th May 2009, 00:51
If your model keeps records from the "persons" table then you can query the model instead of asking the database. I'm not convinced this will help though.

jon-ecm
7th May 2009, 01:06
Thanks! I tried what you said but it didn't help
Thank you so much for helping me though! This is the first time I'v ever even been anwsered on this fourm!

If you have any ideas though I'd love it!

jon-ecm
7th May 2009, 05:00
I got it working, at least I think so :) lol

Here's the function I'm using now!


for(int i = 0; i <= model->rowCount();i++)
{
if(model->data(model->index(i,1)).toString() == searchTag){
qDebug() << "Found Match At: " << i;
barcodeEdit->setText("");
model->setData(model->index(i,6),model->data(model->index(i,6)).toInt() + 1);//Add one to the days attended field that corresponds to the user
model->setData(model->index(i,5),model->data(model->index(i,5)).toString() + QDateTime::currentDateTime().toString() + '\n');//Add one to the dates attended field that corresponds to the user
break;
}
}

It's very unpolished right now and I need to add error catching but that's the idea! If you see anything that I'm doing wrong Please let me know!

spirit
7th May 2009, 06:03
maybe it's better to use QAbstractItemModel::match for seching items?

jon-ecm
7th May 2009, 06:30
Thanks! I hadn't noticed that! I'll give it a shot!