PDA

View Full Version : How to replace string in qt



lekhrajdeshmukh
21st October 2011, 13:50
Hi All,

Need your help on urgent basis , as i am trying to replace the below string with respective value.Actually i am beginner on QT platform and i used replace function of qstring but it doesn't worked.Please find th below query on which i want to put specific value.
Query
select distinct(a.CALLING_NUMBER),b.cdr_details,a.CALLING _IMSI,a.CALLING_IMEI,a.CALLED_IMSI,a.CALLED_NUMBER ,a.CALLED_IMEI,a.CALLED_NUMBER_TON,a.LOC_AREA_CODE ,a.CELL_ID,a.MSC_ID,convert(varchar,start_time,105 ) +' '+ convert(varchar,start_time,108),a.DURATION,a.MSRN, LATITUDE,LONGITUDE,LOC_DETAIL from tbl_cdr_loader a,tbl_cdr_type_detail b,tbl_location_detail c where a.cdr_type=b.cdr_type and a.loc_area_code=c.loc_code and a.cell_id=c.cell_id and a.calling_number in ('<>','<>','<>','<>','<>')

Here I want to replace each angle brackets (<>) with specific value.I have these five input values "9831024841|9831024844|9831024843|9831024842|983102 4846" and it is seperated by |.

So atlast I want my output on below format.

select distinct(a.CALLING_NUMBER),b.cdr_details,a.CALLING _IMSI,a.CALLING_IMEI,a.CALLED_IMSI,a.CALLED_NUMBER ,a.CALLED_IMEI,a.CALLED_NUMBER_TON,a.LOC_AREA_CODE ,a.CELL_ID,a.MSC_ID,convert(varchar,start_time,105 ) +' '+ convert(varchar,start_time,108),a.DURATION,a.MSRN, LATITUDE,LONGITUDE,LOC_DETAIL from tbl_cdr_loader a,tbl_cdr_type_detail b,tbl_location_detail c where a.cdr_type=b.cdr_type and a.loc_area_code=c.loc_code and a.cell_id=c.cell_id and a.calling_number in ('9831024841','9831024844','9831024843','983102484 2','9831024842')

Request you all to please help me on urgent basis.

Thanks in advcance.

Regards,
Lekhraj Deshmukh

totem
21st October 2011, 14:12
You will have to split the input values string, and iterate on your input query to replace each occurence of brackets.
I did not compile or test it, but here is the idea :



// input query
QString query = "select distinct(a.CALLING_NUMBER),b.cdr_details,a.CALLING _IMSI,a.CALLING_IMEI,a.CALLED_IMSI,a.CALLED_NUMBER ,a.CALLED_IMEI,a.CALLED_NUMBER_TON,a.LOC_AREA_CODE ,a.CELL_ID,a.MSC_ID,convert(varchar,start_time,105 ) +' '+ convert(varchar,start_time,108),a.DURATION,a.MSRN, LATITUDE,LONGITUDE,LOC_DETAIL from tbl_cdr_loader a,tbl_cdr_type_detail b,tbl_location_detail c where a.cdr_type=b.cdr_type and a.loc_area_code=c.loc_code and a.cell_id=c.cell_id and a.calling_number in ('<>','<>','<>','<>','<>')" ;
// input numbers
QString inputNumbers = "9831024841|9831024844|9831024843|9831024842|983102 4846" ;
// split this string to retrieve actual numbers
QStringList numbers = inputNumbers.split("|", QString::SkipEmptyParts) ;

// iterate on query and replace "on the fly"
foreach(const QString & number, numbers)
{
// find first occurence of brackets in query
int index = query.indexOf("<>") ;
if(index>=0)
{
// replace two characters ("<>") by the corresponding number
query.replace(index,2,number) ;
}
}


you will have to make sure input data is consistent, or add some check here and there to handle errors

wysota
21st October 2011, 16:26
See QSqlQuery::prepare() and QSqlQuery::bindValue()

lekhrajdeshmukh
24th October 2011, 09:19
Hi Totem,

Thanks for your reply.This solution helped me to solve my problem.

Thanks once again for your reply :)

Regards,
Lekhraj