PDA

View Full Version : QDataTable setFilter



cristiano
3rd November 2006, 22:40
Hello,

I need to make one update in the filtered data of the QDataTable more I am not obtaining, would like to store setFilter in an variable to execute in the SQL.

/******************************************/
void FormTest::primeUpdateValuesData()
{
dataTable->setFilter("id_test = id_test");
int i = dataTable->currentRow();
if ( i == -1 ) i = 0;

/* it would like to effect update of id_test in clause SQL WHERE */
QSqlQuery query;
query.exec( "UPDATE tabletest SET date_time=now() WHERE id_test;" );

dataTable->refresh(QDataTable::RefreshData);
}
/******************************************/

Cristiano

jacek
3rd November 2006, 22:57
it would like to effect update of id_test in clause SQL WHERE
Do you mean something like this?

query.exec( "UPDATE tabletest SET date_time=now() WHERE " + dataTable->filter() );
or

query.prepare( "UPDATE tabletest SET date_time=now() WHERE id_test = :id" );
query.bindValue( ":id", ... );
query.exec();

cristiano
4th November 2006, 00:20
Yes, Now it is working.

query.exec( "UPDATE tabletest SET date_time=now() WHERE " + dataTable->filter() );

Thanks Jacek

Cristiano

jacek
4th November 2006, 01:41
Just beware, that if user can use any string as the filter, he can mess up your database through SQL injection.

cristiano
16th November 2006, 18:03
Hi,

I have in mine form of accompaniment, type three occurrences with the same ID, when I make query below only functions when I have "1 occurrence", more he will be had more than one, query modifies all the fields.

Here it functions only for one.

queryVtr.exec( "UPDATE viatures SET dt_tm_chg=now() WHERE " + dataTable->filter() );

Query SQL would be this,

UPDATE viatures SET dt_tm_chg=now() WHERE id_num_ocor=???? AND id_viature_ocor=???? AND id_city_ocor=????

The possibility of the use of setFilter() exists to filter the fields of the QDataTable.

jacek
16th November 2006, 19:39
Sorry, I don't quite follow you.

cristiano
16th November 2006, 20:22
Sorry for the bad English

With this command below, when I have equal ID, and I effect one update it modifies the date for the two registers.

void Form::updateDataVtr()
{
queryVtr.exec( "UPDATE viatures SET dt_tm_chg=now() WHERE " + dataTable->filter() );
}

It has the example here:

http://200.193.29.195/qt/form_app.png

it would not like that ocorrece this, I only want to change 1 register

jacek
16th November 2006, 21:04
Then maybe you should try this way:

QSqlQuery q;
q.prepare( "UPDATE viatures SET dt_tm_chg=now() WHERE id_num_ocor= :id_num_ocor "
" AND id_viature_ocor= :id_viature_ocor AND id_city_ocor= :id_city_ocor" );
q.bindValue( ":id_num_ocor", ... );
q.bindValue( ":id_viature_ocor", ... );
q.bindValue( ":id_city_ocor", ... );
q.exec();

cristiano
16th November 2006, 22:19
Yes !

More as I read the variable of the QDataTable? setFilter() does not filter the field.

q.bindValue( ":id_num_ocor", ... );

jacek
16th November 2006, 22:25
You can retrieve field values through QDataTable::currentRecord() or QDataTable::value().

cristiano
17th November 2006, 01:38
Thanks Jacek,

functioned using QDataTable::value()

/*****************************************/
QVariant getCurrentItemId;
getCurrentItemId= myDataTable->value(0,0);
...
..
query.prepare( "UPDATE ...." );
query.bindValue( "::id_num_ocor", getCurrentItemId );
...
...
query.exec();
/*****************************************/