HOW-TO:Update SQL databases when files move

XBMC has functions to fully manage its database files. Some advanced operations can be performed by editing the database tables directly on your PC. Be forewarned that database tables are relational (inter-related) - do not change any entries unless you are sure of the implications! Make a backup first, and worst-case, you just have to delete the corrupt database file and rescan your music, videos, or programs from within XBMC.

If you are using a shared mySQL Database on a NAS, please follow this guide instead:
 * HOW-TO:Share libraries using MySQL/Update Paths In MySQL

Required Software

Two popular SQLite interfaces for Windows are:
 * SQLite3Explorer
 * SQLite Spy

A capable but unmaintained SQLite client for OSX can be found here:
 * Mike T's SQLite Database Administrator Tool

A client for Linux (works on other *nix as well):
 * Sqliteman

Assumptions
Let's say you've got all your music stored on shared folder from a PC or NAS (network attached storage), and you've scanned it into the MyMusic##.db database. You then changed a drive location, a computer name, etc.:

Copy UserData\Database\MyMusic##.db over to your PC, run SQLite3Explorer, and examine the tables. You'll see that the paths in the table name 'paths', in a field named strPath.

original location: smb://my_nas/old_share/Music/Library/...rest of path/filenames

new location: smb://my_nas/new_share/Music/Library/...rest of path/filenames

Understanding SQLite text replacement
Here are two alternative ways to update paths in your database; they're presented here for your consideration. You can just generally just follow the instructions in the sections below, however.

Using the REPLACE function:
It's important to realize that REPLACE will replace every occurrence of the search string, so it's recommended that you use include the beginning of the URI (like smb://...)

UPDATE path SET strPath = REPLACE(strPath,'smb://my_nas/old_share', 'smb://my_nas/new_share');

Alternately, using the SUBSTR function:
If you want to have guaranteed control over which characters get replaced, you can use the SUBSTR function.

UPDATE path SET strPath = SUBSTR(strPath,1,13) || 'new_share' || SUBSTR(strPath,23);

You can see that the table named 'path' was updated to change the values in the field named 'strPath': * we took the first 13 characters of the original string "smb://my_nas/" * appended the string "new_share" * appended the 20th-onwards part of the original string to the end: "/Music/Library/..."

So, the SUBSTR function returns a substring from an original string, taking these parameters: * original string to operate on  * position of first character to extract * total number of characters to extract

And the LENGTH function determines the number of characters in the original string; you can see that in this example how many characters remain after discarding the first 16.

Updating your Music library
Open the latest MyMusic*.db file in your sqlite program, and run the following command (substituting your paths as appropriate): UPDATE path SET strPath = REPLACE(strPath,'smb://my_nas/old_share', 'smb://my_nas/new_share');

Updating your Video library
Open the latest MyVideo*.db file in your sqlite program, and run the following command (substituting your paths as appropriate): UPDATE path SET strPath = REPLACE(strPath,'smb://my_nas/old_share', 'smb://my_nas/new_share'); UPDATE movie SET c22 = REPLACE(c22,'smb://my_nas/old_share', 'smb://my_nas/new_share'); UPDATE episode SET c18 = REPLACE(c18,'smb://my_nas/old_share', 'smb://my_nas/new_share'); UPDATE art SET url = REPLACE(url,'smb://my_nas/old_share', 'smb://my_nas/new_share'); UPDATE tvshow SET c16 = REPLACE(c16,'smb://my_nas/old_share', 'smb://my_nas/new_share');

Open the latest Textures*.db file in your sqlite program, and run the following command (substituting your paths as appropriate): UPDATE path SET url = REPLACE(url,'smb://my_nas/old_share', 'smb://my_nas/new_share');

Updating sources.xml
Edit sources.xml in a text editor and do a global search-and-replace for smb://my_nas/old_share to smb://my_nas/new_share