Use this skill when you need to execute SQL against the MoviePilot database. This skill guides you through connecting to the database and executing SQL statements. The database type (SQLite or PostgreSQL) and connection details are provided in the system prompt <system_info>. Applicable scenarios include: 1) The user asks about data statistics, counts, or aggregations that existing tools don't cover; 2) The user wants to inspect, modify, or fix raw database records; 3) The user asks to clean up data, update records, or perform database maintenance; 4) The user asks questions like "how many downloads", "show me site stats", "delete old records", etc.
This skill guides you through executing SQL against the MoviePilot database. Both read and write operations are supported.
You need the following tools:
execute_command - Execute shell commands to run database queriesThe system prompt <system_info> section already contains all the database connection details you need:
sqlite or postgresqlSQLite (/config/db/moviepilot.db)PostgreSQL (user:password@host:port/database)Do NOT run any detection commands. Extract the database type and connection details directly from <system_info>.
Extract the database file path from <system_info> (the path inside the parentheses after SQLite).
Use execute_command to run queries:
sqlite3 -header -column <DB_PATH> "YOUR SQL QUERY HERE;"
For JSON-formatted output (easier to parse):
sqlite3 -json <DB_PATH> "YOUR SQL QUERY HERE;"
List all tables:
sqlite3 -header -column <DB_PATH> "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"
View table schema:
sqlite3 <DB_PATH> ".schema tablename"
Extract the connection parameters from <system_info> (parse user:password@host:port/database from the parentheses after PostgreSQL).
Use execute_command to run queries via psql:
PGPASSWORD=<password> psql -h <host> -p <port> -U <user> -d <database> -c "YOUR SQL QUERY HERE;"
List all tables:
PGPASSWORD=<password> psql -h <host> -p <port> -U <user> -d <database> -c "SELECT tablename FROM pg_tables WHERE schemaname='public' ORDER BY tablename;"
View table schema:
PGPASSWORD=<password> psql -h <host> -p <port> -U <user> -d <database> -c "\d tablename"
After executing the query, analyze the results and present them in a clear, user-friendly format. Use aggregation, sorting, and filtering as needed.
MoviePilot uses the following core tables:
Key columns: id, path, type, title, year, tmdbid, imdbid, doubanid, seasons, episodes, downloader, download_hash, torrent_name, torrent_site, userid, username, date, media_category
Key columns: id, downloader, download_hash, fullpath, savepath, filepath, torrentname, state
Key columns: id, src, dest, mode, type, category, title, year, tmdbid, seasons, episodes, download_hash, status (boolean: true=success, false=failed), errmsg, date
Key columns: id, name, year, type, tmdbid, doubanid, season, total_episode, start_episode, lack_episode, state ('N'=new, 'R'=running, 'S'=paused), filter, include, exclude, quality, resolution, sites, best_version, date, username
Key columns: id, name, year, type, tmdbid, doubanid, season, total_episode, start_episode, date, username
Key columns: id, name, email, is_active, is_superuser, permissions, settings
Key columns: id, name, domain, url, pri (priority), cookie, proxy, is_active, downloader, limit_interval, limit_count
Key columns: id, domain, name, username, user_level, bonus, upload, download, ratio, seeding, leeching, seeding_size, updated_day
Key columns: id, domain, success, fail, seconds, lst_state, lst_mod_date
Key columns: id, server, library, item_id, item_type, title, original_title, year, tmdbid, imdbid, tvdbid, path
Key columns: id, key, value (JSON)
Key columns: id, username, key, value (JSON)
Key columns: id, plugin_id, key, value (JSON)
Key columns: id, channel, source, mtype, title, text, image, link, userid, reg_time
Key columns: id, name, description, timer, trigger_type, event_type, state ('W'=waiting, 'R'=running), run_count, actions, flows, last_time
Key columns: id, user_id, credential_id, public_key, name, created_at, last_used_at, is_active
Key columns: id, name, domain, url, base64
SELECT COUNT(*) AS total FROM downloadhistory;
SELECT title, year, type, torrent_site, date FROM downloadhistory ORDER BY id DESC LIMIT 10;
SELECT id, title, src, errmsg, date FROM transferhistory WHERE status = 0 ORDER BY id DESC LIMIT 10;
SELECT name, year, type, season, state, lack_episode FROM subscribe WHERE state = 'R';
SELECT name, domain, upload, download, ratio, bonus, seeding, user_level FROM siteuserdata ORDER BY upload DESC;
SELECT server, library, COUNT(*) AS count FROM mediaserveritem GROUP BY server, library;
SELECT domain, success, fail, ROUND(success * 100.0 / (success + fail), 1) AS success_rate FROM sitestatistic WHERE success + fail > 0 ORDER BY success_rate DESC;
SELECT plugin_id, key FROM plugindata ORDER BY plugin_id, key;
DELETE FROM downloadhistory WHERE date < '2024-01-01';
UPDATE subscribe SET state = 'S' WHERE id = 123;
DELETE FROM transferhistory WHERE status = 0 AND date < '2024-06-01';
INSERT, UPDATE, DELETE, DROP, ALTER, or TRUNCATE operation, always describe what the statement will do and ask the user to confirm before executing. For SELECT queries, execute directly without confirmationDELETE, DROP, or TRUNCATE on important tables, suggest the user back up the data first (e.g., export with .dump for SQLite or pg_dump for PostgreSQL)UPDATE or DELETE without a WHERE clause unless the user explicitly intends to affect all rowsSELECT, add LIMIT to prevent excessive outputsite table contains cookie, apikey, and token fields. NEVER display these values to the user. Exclude them from SELECT or replace with '***'user table contains hashed_password and otp_secret fields. NEVER display these valuesWhen writing queries, be aware of differences between SQLite and PostgreSQL:
| Feature | SQLite | PostgreSQL |
|---|---|---|
| Boolean values | 0 / 1 | false / true |
| String concat | || | || or CONCAT() |
| Current time | datetime('now') | NOW() |
| LIMIT syntax | LIMIT n | LIMIT n |
| JSON access | json_extract(col, '$.key') | col->>'key' |
| Case sensitivity | Case-insensitive by default | Case-sensitive |
| LIKE | Case-insensitive | Use ILIKE for case-insensitive |
sqlite3 CLI should be pre-installed in the MoviePilot Docker container. If missing, you can try using Python: python3 -c "import sqlite3; ..."psql is not available, use Python: python3 -c "import psycopg2; ..."