Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update an SQLite database with a search and replace query?

My SQL knowledge is very limited, specially about SQLite, although I believe this is will be some sort of generic query... Or maybe not because of the search and replace...

I have this music database in SQLite that has various fields of course but the important ones here the "media_item_id" and "content_url".

Here's an example of a "content_url":

file:///c:/users/nazgulled/music/band%20albums/devildriver/%5b2003%5d%20devildriver/08%20-%20what%20does%20it%20take%20(to%20be%20a%20man).mp3 

I'm looking for a query that will search for entries like those, where "content_url" follows that pattern and replace it (the "content_url") with something else.

For instance, a generic "content_url" can be this:

file:///c:/users/nazgulled/music/band%20albums/BAND_NAME/ALBUM_NAME/SONG_NAME.mp3 

And I want to replace all these entries with:

file:///c:/users/nazgulled/music/bands/studio%20albums/BAND_NAME/ALBUM_NAME/SONG_NAME.mp3 

How can I do it in one query?

P.S: I'm using Firefox SQLite Manager (couldn't find a better and free alternative for Windows).

like image 728
rfgamaral Avatar asked Jun 30 '09 04:06

rfgamaral


People also ask

How do I create a SQLite database update?

Introduction to SQLite UPDATE statement First, specify the table where you want to update after the UPDATE clause. Second, set new value for each column of the table in the SET clause. Third, specify rows to update using a condition in the WHERE clause. The WHERE clause is optional.

Which method is used to insert update and modify data in SQLite?

Parameterized Queries: These are those queries which are performed using inbuilt functions to insert, read, delete or update data.

Does SQLite support select for update?

For example, MySQL supports SELECT FOR UPDATE, but SQLite does not.

Which SQLite statement is used to update data into table?

The SQLite UPDATE statement is used to update existing records in a table in a SQLite database. There are 2 syntaxes for the UPDATE statement depending on the type of update that you wish to perform.


1 Answers

You are probably looking for the replace function.

For example,

update table_name set    content_url = replace(content_url, 'band%20albums', 'bands/studio%20albums') where   content_url like '%nazgulled/music/band_20albums/%'; 

More documentation at http://sqlite.org/lang_corefunc.html

like image 129
laalto Avatar answered Sep 25 '22 17:09

laalto