PDA

View Full Version : Cutting Elements QStringlist



codeman
5th June 2009, 11:43
Hello friends I have to cut elements before transfer to DB:

My approach is



int i_fieldlength[]={4,2,4,40,8,6,5,3,8,1,5,3,7,8};
for(int i =0; i < sizeof(i_fieldlength);i++){
QString strbuf = splitlinelist.at(i);
splitlinelist.at(i)=strbuf.left(i_fieldlength[i]);
}


Is there a more elegant way or faster way to do this???

Ups this approach create an error:


sources\tableeditor.cpp|917|error: passing `const QString' as `this' argument of `QString& QString::operator=(const QString&)' discards qualifiers|

Lykurg
5th June 2009, 11:55
You can use the database build in functions if your strings aren't too long. MySQL e.g supports
q.prepare("...SET name = SUBSTRING(:str,0,:len)...");
for(int i =0; i < sizeof(i_fieldlength)/sizeof(i_fieldlength[0]);++i)
{
q.bindValue(":str", splitlinelist.at(i));
q.bindValue(":len", i_fieldlength[i]);
q.exec();
}

NOTE: sizeof(i_fieldlength)/sizeof(i_fieldlength[0]) and ++i!

EDIT: splitlinelist.at(i) => at() is constant so you can't assign a value to that. If you want to use the [] operator.

codeman
5th June 2009, 12:06
I use mssql !

And I do an insert but before I would like to cut the list...

Lykurg
5th June 2009, 12:19
EDIT: splitlinelist.at(i) => at() is constant so you can't assign a value to that. If you want to use the [] operator.

And I do an insert but before I would like to cut the list...

then use the above mentioned:


splitlinelist[i] = splitlinelist.at(i).left(i_fieldlength[i]);