¿Cómo reemplazar una cadena en SQLite?

How to update table column having this:


y reemplazarlo con


in SQLite?

Note: All text, except /Documents/ es dinámico.

preguntado el 01 de febrero de 12 a las 14:02

what you have tried? where are you stuck? What are you using to access sqlite table core data or c sqlite3.h -

I do not know how to replace the string which gets dyanmically generated. That is where I am stuck. Can you please help? -

Why are you positing similar question again and again? Duplicate : stackoverflow.com/questions/9108517/… -

4 Respuestas

You have to read first the string and put it in to an nsstring and then generate newstring form the old one and then update in to db.

Here you have the objective c code for generate the new string. The db update you can doit with the other answers.

    NSString *originalString = @"/var/mobile/233KKFSDK3234/Documents/Page.jpg";

    NSRange documentsRage = [originalString rangeOfString:@"/Documents/"];

    NSString *newString = [NSString stringWithString:[originalString substringWithRange:NSMakeRange(documentsRage.location, [originalString length]-documentsRage.location)]];

Respondido 01 Feb 12, 20:02

UPDATE table SET column = IF(
    POSITION('/Documents/' IN column),
    RIGHT(column, (LENGTH(column) - POSITION('/Documents/' IN column) - LENGTH('/Documents/') + 1)),

If the pattern is found, only keep characters right of the occurrence; else, do nothing (keep old value).

Respondido 01 Feb 12, 23:02

Thanks for this, but '/var/mobile/233KKFSDK3234' is dyamically generated, so it is not fixed. Only thing that is fixed is /Documents/Page.jpg. In that case, what should I do? - Meetpd

if /Documents/Page.jpg is fixed, why don't you just set all to /Documents/Page.jpg? - Christian Schnorr

My apology but all text except /Documents/ is dynamic. I have edited the question. Sorry could not explain properly first time. - Meetpd

No, it gives error when I run this query. I replaced COLUMN with name of the Column and table with Table Name. Can you please confirm? - Meetpd

Lo siguiente funcionó para mí:

UPDATE yourtable SET yourfield = SUBSTR(yourfield,18);

..as stated by Bjoern on this question: How to Update this data using SQL?

contestado el 23 de mayo de 17 a las 15:05

Didn't he say '/var/mobile/233KKFSDK3234/' is dynamic? - Christian Schnorr

Do the string processing in ObjC and then Use this query.

update tablename set columnname = 'processedString' ;

Updated as per your requirement

Respondido 18 ago 13, 21:08

My apology but all text except /Documents/ is dynamic. I have edited the question. Sorry could not explain properly first time. - Meetpd

You assume that the new string is Documents/Page.jpg and not any other image or string - Ronny Sherer

@RonnySherer, In Sqlite you can only use SUBSTR(). Since he mentioned the entire string except /Documents/ it is not possible to use that function as well. Hence I assumed he would do the string processing in ObjC and update the column as required. But I haven't mentioned it in the answer previously. I appreciate your effort to correct it. I edited the answer. Thanks. - Vignesh

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.