HOW-TO:Update SQL databases when files move

From Official Kodi Wiki
Revision as of 07:40, 4 May 2021 by Klojum (talk | contribs) (→‎Updating your Video library)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Kodi 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 Kodi.

If you are using a shared mySQL Database on a NAS, please follow this guide instead: HOW-TO:Update_Paths_In_MySQL

Required Software

Two popular SQLite interfaces for Windows are:

A capable but unmaintained SQLite client for OSX can be found here:

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

1 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/ of path/filenames

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

2 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.

2.1 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');

2.2 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.

3 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');

4 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');
  UPDATE files SET strFilename = REPLACE(strFilename,'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');
  UPDATE path SET texture = REPLACE(texture,'smb://my_nas/old_share', 'smb://my_nas/new_share');

Additionally, if you're updating from a local Windows path to a smb share, you will need to replace backslashes with forward slashes in each of the tables in both databases in order to prevent a future library update from treating the discovered files as different from the existing. For example:

  UPDATE path SET strPath = REPLACE(strPath,'\', '/');

5 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