database-operation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDatabase Query (数据库查询)
数据库查询 (Database Query)
This skill guides you through executing SQL against the MoviePilot database. Both read and write operations are supported.
本Skill将引导你对MoviePilot数据库执行SQL操作,支持读写两种操作。
Prerequisites
前提条件
You need the following tools:
- - Execute shell commands to run database queries
execute_command
你需要以下工具:
- - 执行Shell命令以运行数据库查询
execute_command
Getting Database Connection Info
获取数据库连接信息
The system prompt section already contains all the database connection details you need:
<system_info>- 数据库类型 — or
sqlitepostgresql - 数据库 — Full connection info:
- For SQLite: the database file path, e.g.
SQLite (/config/db/moviepilot.db) - For PostgreSQL: the connection string, e.g.
PostgreSQL (user:password@host:port/database)
- For SQLite: the database file path, e.g.
Do NOT run any detection commands. Extract the database type and connection details directly from .
<system_info>系统提示<system_info>部分已包含你所需的所有数据库连接详情:
- 数据库类型 — 或
sqlitepostgresql - 数据库 — 完整连接信息:
- 对于SQLite:数据库文件路径,例如
SQLite (/config/db/moviepilot.db) - 对于PostgreSQL:连接字符串,例如
PostgreSQL (user:password@host:port/database)
- 对于SQLite:数据库文件路径,例如
请勿运行任何检测命令,直接从<system_info>中提取数据库类型和连接详情。
Executing Queries
执行查询
SQLite Mode
SQLite模式
Extract the database file path from (the path inside the parentheses after ).
<system_info>SQLiteUse to run queries:
execute_commandbash
sqlite3 -header -column <DB_PATH> "YOUR SQL QUERY HERE;"For JSON-formatted output (easier to parse):
bash
sqlite3 -json <DB_PATH> "YOUR SQL QUERY HERE;"List all tables:
bash
sqlite3 -header -column <DB_PATH> "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"View table schema:
bash
sqlite3 <DB_PATH> ".schema tablename"从<system_info>中提取数据库文件路径(后括号内的路径)。
SQLite使用运行查询:
execute_commandbash
sqlite3 -header -column <DB_PATH> "YOUR SQL QUERY HERE;"如需JSON格式输出(更易解析):
bash
sqlite3 -json <DB_PATH> "YOUR SQL QUERY HERE;"列出所有表:
bash
sqlite3 -header -column <DB_PATH> "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"查看表结构:
bash
sqlite3 <DB_PATH> ".schema tablename"PostgreSQL Mode
PostgreSQL模式
Extract the connection parameters from (parse from the parentheses after ).
<system_info>user:password@host:port/databasePostgreSQLUse to run queries via :
execute_commandpsqlbash
PGPASSWORD=<password> psql -h <host> -p <port> -U <user> -d <database> -c "YOUR SQL QUERY HERE;"List all tables:
bash
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:
bash
PGPASSWORD=<password> psql -h <host> -p <port> -U <user> -d <database> -c "\d tablename"从<system_info>中提取连接参数(解析后括号内的)。
PostgreSQLuser:password@host:port/database使用通过运行查询:
execute_commandpsqlbash
PGPASSWORD=<password> psql -h <host> -p <port> -U <user> -d <database> -c "YOUR SQL QUERY HERE;"列出所有表:
bash
PGPASSWORD=<password> psql -h <host> -p <port> -U <user> -d <database> -c "SELECT tablename FROM pg_tables WHERE schemaname='public' ORDER BY tablename;"查看表结构:
bash
PGPASSWORD=<password> psql -h <host> -p <port> -U <user> -d <database> -c "\d tablename"Interpret Results
结果解读
After executing the query, analyze the results and present them in a clear, user-friendly format. Use aggregation, sorting, and filtering as needed.
执行查询后,分析结果并以清晰、友好的格式呈现给用户。按需使用聚合、排序和过滤操作。
Database Schema Reference
数据库结构参考
MoviePilot uses the following core tables:
MoviePilot使用以下核心表:
downloadhistory (下载历史)
downloadhistory(下载历史)
Key columns: , , , , , , , , , , , , , , , , ,
idpathtypetitleyeartmdbidimdbiddoubanidseasonsepisodesdownloaderdownload_hashtorrent_nametorrent_siteuseridusernamedatemedia_category关键列:, , , , , , , , , , , , , , , , ,
idpathtypetitleyeartmdbidimdbiddoubanidseasonsepisodesdownloaderdownload_hashtorrent_nametorrent_siteuseridusernamedatemedia_categorydownloadfiles (下载文件)
downloadfiles(下载文件)
Key columns: , , , , , , ,
iddownloaderdownload_hashfullpathsavepathfilepathtorrentnamestate关键列:, , , , , , ,
iddownloaderdownload_hashfullpathsavepathfilepathtorrentnamestatetransferhistory (整理历史)
transferhistory(整理历史)
Key columns: , , , , , , , , , , , , (boolean: true=success, false=failed), ,
idsrcdestmodetypecategorytitleyeartmdbidseasonsepisodesdownload_hashstatuserrmsgdate关键列:, , , , , , , , , , , , (布尔值:true=成功,false=失败), ,
idsrcdestmodetypecategorytitleyeartmdbidseasonsepisodesdownload_hashstatuserrmsgdatesubscribe (订阅)
subscribe(订阅)
Key columns: , , , , , , , , , , ('N'=new, 'R'=running, 'S'=paused), , , , , , , , ,
idnameyeartypetmdbiddoubanidseasontotal_episodestart_episodelack_episodestatefilterincludeexcludequalityresolutionsitesbest_versiondateusername关键列:, , , , , , , , , , ('N'=新建,'R'=运行中,'S'=暂停), , , , , , , , ,
idnameyeartypetmdbiddoubanidseasontotal_episodestart_episodelack_episodestatefilterincludeexcludequalityresolutionsitesbest_versiondateusernamesubscribehistory (订阅历史)
subscribehistory(订阅历史)
Key columns: , , , , , , , , , ,
idnameyeartypetmdbiddoubanidseasontotal_episodestart_episodedateusername关键列:, , , , , , , , , ,
idnameyeartypetmdbiddoubanidseasontotal_episodestart_episodedateusernameuser (用户)
user(用户)
Key columns: , , , , , ,
idnameemailis_activeis_superuserpermissionssettings关键列:, , , , , ,
idnameemailis_activeis_superuserpermissionssettingssite (站点)
site(站点)
Key columns: , , , , (priority), , , , , ,
idnamedomainurlpricookieproxyis_activedownloaderlimit_intervallimit_count关键列:, , , , (优先级), , , , , ,
idnamedomainurlpricookieproxyis_activedownloaderlimit_intervallimit_countsiteuserdata (站点用户数据)
siteuserdata(站点用户数据)
Key columns: , , , , , , , , , , , ,
iddomainnameusernameuser_levelbonusuploaddownloadratioseedingleechingseeding_sizeupdated_day关键列:, , , , , , , , , , , ,
iddomainnameusernameuser_levelbonusuploaddownloadratioseedingleechingseeding_sizeupdated_daysitestatistic (站点统计)
sitestatistic(站点统计)
Key columns: , , , , , ,
iddomainsuccessfailsecondslst_statelst_mod_date关键列:, , , , , ,
iddomainsuccessfailsecondslst_statelst_mod_datemediaserveritem (媒体库条目)
mediaserveritem(媒体库条目)
Key columns: , , , , , , , , , , ,
idserverlibraryitem_iditem_typetitleoriginal_titleyeartmdbidimdbidtvdbidpath关键列:, , , , , , , , , , ,
idserverlibraryitem_iditem_typetitleoriginal_titleyeartmdbidimdbidtvdbidpathsystemconfig (系统配置)
systemconfig(系统配置)
Key columns: , , (JSON)
idkeyvalue关键列:, , (JSON格式)
idkeyvalueuserconfig (用户配置)
userconfig(用户配置)
Key columns: , , , (JSON)
idusernamekeyvalue关键列:, , , (JSON格式)
idusernamekeyvalueplugindata (插件数据)
plugindata(插件数据)
Key columns: , , , (JSON)
idplugin_idkeyvalue关键列:, , , (JSON格式)
idplugin_idkeyvaluemessage (消息)
message(消息)
Key columns: , , , , , , , , ,
idchannelsourcemtypetitletextimagelinkuseridreg_time关键列:, , , , , , , , ,
idchannelsourcemtypetitletextimagelinkuseridreg_timeworkflow (工作流)
workflow(工作流)
Key columns: , , , , , , ('W'=waiting, 'R'=running), , , ,
idnamedescriptiontimertrigger_typeevent_typestaterun_countactionsflowslast_time关键列:, , , , , , ('W'=等待,'R'=运行中), , , ,
idnamedescriptiontimertrigger_typeevent_typestaterun_countactionsflowslast_timepasskey (通行密钥)
passkey(通行密钥)
Key columns: , , , , , , ,
iduser_idcredential_idpublic_keynamecreated_atlast_used_atis_active关键列:, , , , , , ,
iduser_idcredential_idpublic_keynamecreated_atlast_used_atis_activesiteicon (站点图标)
siteicon(站点图标)
Key columns: , , , ,
idnamedomainurlbase64关键列:, , , ,
idnamedomainurlbase64Common Query Examples
常见查询示例
Count total downloads
统计总下载量
sql
SELECT COUNT(*) AS total FROM downloadhistory;sql
SELECT COUNT(*) AS total FROM downloadhistory;Recent download history
最近下载历史
sql
SELECT title, year, type, torrent_site, date FROM downloadhistory ORDER BY id DESC LIMIT 10;sql
SELECT title, year, type, torrent_site, date FROM downloadhistory ORDER BY id DESC LIMIT 10;Failed transfers
失败的整理记录
sql
SELECT id, title, src, errmsg, date FROM transferhistory WHERE status = 0 ORDER BY id DESC LIMIT 10;sql
SELECT id, title, src, errmsg, date FROM transferhistory WHERE status = 0 ORDER BY id DESC LIMIT 10;Active subscriptions
活跃订阅
sql
SELECT name, year, type, season, state, lack_episode FROM subscribe WHERE state = 'R';sql
SELECT name, year, type, season, state, lack_episode FROM subscribe WHERE state = 'R';Site upload/download statistics
站点上传/下载统计
sql
SELECT name, domain, upload, download, ratio, bonus, seeding, user_level FROM siteuserdata ORDER BY upload DESC;sql
SELECT name, domain, upload, download, ratio, bonus, seeding, user_level FROM siteuserdata ORDER BY upload DESC;Media library statistics
媒体库统计
sql
SELECT server, library, COUNT(*) AS count FROM mediaserveritem GROUP BY server, library;sql
SELECT server, library, COUNT(*) AS count FROM mediaserveritem GROUP BY server, library;Site access success rate
站点访问成功率
sql
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;sql
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;Plugin data inspection
插件数据查看
sql
SELECT plugin_id, key FROM plugindata ORDER BY plugin_id, key;sql
SELECT plugin_id, key FROM plugindata ORDER BY plugin_id, key;Delete old download history (write operation)
删除旧下载历史(写入操作)
sql
DELETE FROM downloadhistory WHERE date < '2024-01-01';sql
DELETE FROM downloadhistory WHERE date < '2024-01-01';Update subscription state (write operation)
更新订阅状态(写入操作)
sql
UPDATE subscribe SET state = 'S' WHERE id = 123;sql
UPDATE subscribe SET state = 'S' WHERE id = 123;Clean up failed transfer records (write operation)
清理失败的整理记录(写入操作)
sql
DELETE FROM transferhistory WHERE status = 0 AND date < '2024-06-01';sql
DELETE FROM transferhistory WHERE status = 0 AND date < '2024-06-01';Safety Rules
安全规则
- Confirm before writing — For any ,
INSERT,UPDATE,DELETE,DROP, orALTERoperation, always describe what the statement will do and ask the user to confirm before executing. ForTRUNCATEqueries, execute directly without confirmationSELECT - Back up before destructive operations — Before executing ,
DELETE, orDROPon important tables, suggest the user back up the data first (e.g., export withTRUNCATEfor SQLite or.dumpfor PostgreSQL)pg_dump - Use WHERE clauses — Never run or
UPDATEwithout aDELETEclause unless the user explicitly intends to affect all rowsWHERE - Use LIMIT for queries — When querying large tables with , add
SELECTto prevent excessive outputLIMIT - Sensitive data — The table contains
site,cookie, andapikeyfields. NEVER display these values to the user. Exclude them from SELECT or replace withtoken'***' - Password data — The table contains
userandhashed_passwordfields. NEVER display these valuesotp_secret - Output limits — If the query results are very long, summarize or truncate them
- 写入前确认 — 对于任何、
INSERT、UPDATE、DELETE、DROP或ALTER操作,务必先说明语句的作用,并在执行前请求用户确认。对于TRUNCATE查询,可直接执行无需确认SELECT - 破坏性操作前备份 — 在对重要表执行、
DELETE或DROP操作前,建议用户先备份数据(例如,SQLite使用TRUNCATE导出,PostgreSQL使用.dump导出)pg_dump - 使用WHERE子句 — 除非用户明确要影响所有行,否则切勿在没有子句的情况下运行
WHERE或UPDATEDELETE - 查询使用LIMIT — 对大表执行查询时,添加
SELECT以避免输出过多内容LIMIT - 敏感数据 — 表包含
site、cookie和apikey字段。绝对不要向用户展示这些值,在SELECT中排除它们或用token替换'***' - 密码数据 — 表包含
user和hashed_password字段。绝对不要展示这些值otp_secret - 输出限制 — 如果查询结果过长,进行总结或截断
SQL Dialect Differences
SQL方言差异
When writing queries, be aware of differences between SQLite and PostgreSQL:
| Feature | SQLite | PostgreSQL |
|---|---|---|
| Boolean values | | |
| String concat | | |
| Current time | | |
| LIMIT syntax | | |
| JSON access | | |
| Case sensitivity | Case-insensitive by default | Case-sensitive |
| LIKE | Case-insensitive | Use |
编写查询时,请注意SQLite和PostgreSQL之间的差异:
| 特性 | SQLite | PostgreSQL |
|---|---|---|
| 布尔值 | | |
| 字符串拼接 | ` | |
| 当前时间 | | |
| LIMIT语法 | | |
| JSON访问 | | |
| 大小写敏感性 | 默认不区分大小写 | 区分大小写 |
| LIKE | 不区分大小写 | 使用 |
Troubleshooting
故障排除
- sqlite3 not found: The CLI should be pre-installed in the MoviePilot Docker container. If missing, you can try using Python:
sqlite3python3 -c "import sqlite3; ..." - psql not found: For PostgreSQL, if is not available, use Python:
psqlpython3 -c "import psycopg2; ..." - Permission denied: Database queries require admin privileges
- Table not found: Use the "list all tables" query first to verify table names
- sqlite3未找到:命令行工具应已预安装在MoviePilot Docker容器中。若缺失,可尝试使用Python:
sqlite3python3 -c "import sqlite3; ..." - psql未找到:对于PostgreSQL,若不可用,使用Python:
psqlpython3 -c "import psycopg2; ..." - 权限被拒绝:数据库查询需要管理员权限
- 表未找到:先使用“列出所有表”查询确认表名