Working with Strings in a database

I have a website I am taking over from a client, which I will fully rebuild at some point, but for now I need to work with what I have.

They have a SINGLE field in the database that is something like this.

```Vehicle```:```Audi A3 Sportback```Colour```:```Black```Comment```:```It's a pretty car, low miles: come see today.``` 

I asked the previous developer why he started and ended the field in 3 backticks and used 3 backticks to separate each record pair and then used 3 backticks and a colon and another 3 backticks as a separator between the key/value.

His answer was that the users were adding comments with slashes, commas, colons, etc and he needed something the user would more than likely never use to land up with an end result of

Vehicle: Audi A3 Sportback
Colour: Black
Comment: It's a pretty car, low miles: come see today.

Now I would like to split that strange string he has into 3 database fields

vehicle colour comment
Audi A3 Sportback Black It’s a pretty car, low miles: come see today.

I would like to do this using a database query, and then a multi update, where it takes the full odd string from the query and adds just the relevant part to the correct column in my table.
Normally I would just do this through Regex in a text editor and dump it all back to my database, however in this case I can’t and kind of need to do it like this.

I am hoping to use the formatters inside the update server action to extract the part i need for each field.

How about updating directly in the database without using Wappler? You can use regex to separate right in the sql command.

2 Likes

I was thinking about that too, but his old code is still writing new lines like that all day till I replace it, so maybe I could make a cron job to run each time the database updates or something.

Or a trigger on insert/update.

1 Like

Never used a trigger before unless it fired a bullet, suppose I may as well learn a new thing today, haha.

Could I use a view? that way i do not alter his table data at all and the view makes the change for me? is that possible.

Not sure about a view. But you’ll always have his original field untouched. Triggers are easy, have a look.

1 Like

Will give it a try, thank you Ken, appreciate the advice, I had skyped my MySQL guy @brian but he is probably busy, so I figured I best ask here, glad a few of you are MySQL proficient.

1 Like

Hey Paul,

If the data is consistent in all the fields you can create a query, then add a repeat and inside the repeat add an insert step. In order to access the different properties use:

  • vehicle: {{str.split("```")[3]}}
  • color: {{str.split("```")[6]}}
  • comment: {{str.split("```")[9]}}

A quick example with setvalue steps:

Screenshot_2

results in:

Screenshot_3

So you can easily use these values in the insert step to put the data in each field :slight_smile:


That’s how the string looks when you split it, and that’s where 3, 6 and 9 come from:

2 Likes

Nice, thanks Teo, thats brilliant, will give it a test.