db
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDatabase Manager
数据库管理器
Query and manage databases across SQLite, PostgreSQL, and MySQL.
对SQLite、PostgreSQL和MySQL数据库进行查询和管理。
Prerequisites
前置条件
Install database CLIs as needed:
bash
undefined根据需要安装数据库CLI工具:
bash
undefinedSQLite (usually pre-installed on macOS/Linux)
SQLite (usually pre-installed on macOS/Linux)
sqlite3 --version
sqlite3 --version
PostgreSQL
PostgreSQL
brew install postgresql
brew install postgresql
or
or
apt install postgresql-client
apt install postgresql-client
MySQL
MySQL
brew install mysql-client
brew install mysql-client
or
or
apt install mysql-client
undefinedapt install mysql-client
undefinedCLI Reference
CLI参考
SQLite
SQLite
bash
undefinedbash
undefinedConnect to database
连接到数据库
sqlite3 database.db
sqlite3 database.db
Execute query
执行查询
sqlite3 database.db "SELECT * FROM users LIMIT 10"
sqlite3 database.db "SELECT * FROM users LIMIT 10"
Output as CSV
以CSV格式输出
sqlite3 -csv database.db "SELECT * FROM users"
sqlite3 -csv database.db "SELECT * FROM users"
Output as JSON (requires sqlite 3.33+)
以JSON格式输出(需要sqlite 3.33+版本)
sqlite3 -json database.db "SELECT * FROM users"
sqlite3 -json database.db "SELECT * FROM users"
Column headers
显示列标题
sqlite3 -header database.db "SELECT * FROM users"
sqlite3 -header database.db "SELECT * FROM users"
Execute SQL file
执行SQL文件
sqlite3 database.db < queries.sql
sqlite3 database.db < queries.sql
Schema commands
架构命令
sqlite3 database.db ".schema"
sqlite3 database.db ".tables"
sqlite3 database.db ".schema users"
undefinedsqlite3 database.db ".schema"
sqlite3 database.db ".tables"
sqlite3 database.db ".schema users"
undefinedPostgreSQL
PostgreSQL
bash
undefinedbash
undefinedConnect
连接
psql postgresql://user:pass@host:5432/dbname
psql postgresql://user:pass@host:5432/dbname
Execute query
执行查询
psql -c "SELECT * FROM users LIMIT 10" postgresql://...
psql -c "SELECT * FROM users LIMIT 10" postgresql://...
Tuples only (no headers)
仅返回数据行(无标题)
psql -t -c "SELECT count(*) FROM users" postgresql://...
psql -t -c "SELECT count(*) FROM users" postgresql://...
No alignment (machine-readable)
无对齐格式(机器可读)
psql -t -A -c "SELECT id,name FROM users" postgresql://...
psql -t -A -c "SELECT id,name FROM users" postgresql://...
Execute SQL file
执行SQL文件
psql -f queries.sql postgresql://...
psql -f queries.sql postgresql://...
List tables
列出所有表
psql -c "\dt" postgresql://...
psql -c "\dt" postgresql://...
Describe table
查看表结构
psql -c "\d users" postgresql://...
psql -c "\d users" postgresql://...
Output format
输出格式
psql -c "SELECT * FROM users" --csv postgresql://...
psql -c "SELECT * FROM users" --html postgresql://...
undefinedpsql -c "SELECT * FROM users" --csv postgresql://...
psql -c "SELECT * FROM users" --html postgresql://...
undefinedMySQL
MySQL
bash
undefinedbash
undefinedConnect
连接
mysql -h host -u user -p dbname
mysql -h host -u user -p dbname
Execute query
执行查询
mysql -h host -u user -p -e "SELECT * FROM users LIMIT 10" dbname
mysql -h host -u user -p -e "SELECT * FROM users LIMIT 10" dbname
Batch mode (no headers)
批处理模式(无标题)
mysql -h host -u user -p -B -e "SELECT * FROM users" dbname
mysql -h host -u user -p -B -e "SELECT * FROM users" dbname
Execute SQL file
执行SQL文件
mysql -h host -u user -p dbname < queries.sql
mysql -h host -u user -p dbname < queries.sql
Show tables
显示所有表
mysql -h host -u user -p -e "SHOW TABLES" dbname
mysql -h host -u user -p -e "SHOW TABLES" dbname
Describe table
查看表结构
mysql -h host -u user -p -e "DESCRIBE users" dbname
undefinedmysql -h host -u user -p -e "DESCRIBE users" dbname
undefinedCommon Operations
通用操作
Schema Inspection
架构检查
SQLite
SQLite
bash
undefinedbash
undefinedAll tables
所有表
sqlite3 db.sqlite ".tables"
sqlite3 db.sqlite ".tables"
Table schema
表结构
sqlite3 db.sqlite ".schema tablename"
sqlite3 db.sqlite ".schema tablename"
All schemas
所有架构
sqlite3 db.sqlite ".schema"
undefinedsqlite3 db.sqlite ".schema"
undefinedPostgreSQL
PostgreSQL
bash
undefinedbash
undefinedAll tables
所有表
psql -c "\dt" $DATABASE_URL
psql -c "\dt" $DATABASE_URL
Table schema
表结构
psql -c "\d tablename" $DATABASE_URL
psql -c "\d tablename" $DATABASE_URL
Table with indexes
带索引的表信息
psql -c "\d+ tablename" $DATABASE_URL
undefinedpsql -c "\d+ tablename" $DATABASE_URL
undefinedMySQL
MySQL
bash
undefinedbash
undefinedAll tables
所有表
mysql -e "SHOW TABLES" -h host -u user -p dbname
mysql -e "SHOW TABLES" -h host -u user -p dbname
Table schema
表结构
mysql -e "DESCRIBE tablename" -h host -u user -p dbname
mysql -e "DESCRIBE tablename" -h host -u user -p dbname
Create statement
表创建语句
mysql -e "SHOW CREATE TABLE tablename" -h host -u user -p dbname
undefinedmysql -e "SHOW CREATE TABLE tablename" -h host -u user -p dbname
undefinedQuery Explanation
查询执行计划
bash
undefinedbash
undefinedSQLite
SQLite
sqlite3 db.sqlite "EXPLAIN QUERY PLAN SELECT * FROM users WHERE email = 'x'"
sqlite3 db.sqlite "EXPLAIN QUERY PLAN SELECT * FROM users WHERE email = 'x'"
PostgreSQL
PostgreSQL
psql -c "EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'x'" $DATABASE_URL
psql -c "EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'x'" $DATABASE_URL
MySQL
MySQL
mysql -e "EXPLAIN SELECT * FROM users WHERE email = 'x'" dbname
undefinedmysql -e "EXPLAIN SELECT * FROM users WHERE email = 'x'" dbname
undefinedData Export
数据导出
bash
undefinedbash
undefinedSQLite to CSV
SQLite导出为CSV
sqlite3 -csv -header db.sqlite "SELECT * FROM users" > users.csv
sqlite3 -csv -header db.sqlite "SELECT * FROM users" > users.csv
PostgreSQL to CSV
PostgreSQL导出为CSV
psql -c "\COPY users TO 'users.csv' CSV HEADER" $DATABASE_URL
psql -c "\COPY users TO 'users.csv' CSV HEADER" $DATABASE_URL
MySQL to CSV
MySQL导出为CSV
mysql -e "SELECT * FROM users" -B dbname | tr '\t' ',' > users.csv
undefinedmysql -e "SELECT * FROM users" -B dbname | tr '\t' ',' > users.csv
undefinedAI-Assisted Query Generation
AI辅助查询生成
Use Gemini to help write queries:
bash
undefined使用Gemini帮助编写查询:
bash
undefinedDescribe what you want
描述你的需求
gemini -m pro -o text -e "" "Write a SQL query to:
- Find all users who signed up in the last 30 days
- Who have made at least one purchase
- Order by purchase count descending
Table schemas:
- users (id, email, created_at)
- purchases (id, user_id, amount, created_at)
Output PostgreSQL-compatible SQL."
undefinedgemini -m pro -o text -e "" "Write a SQL query to:
- Find all users who signed up in the last 30 days
- Who have made at least one purchase
- Order by purchase count descending
Table schemas:
- users (id, email, created_at)
- purchases (id, user_id, amount, created_at)
Output PostgreSQL-compatible SQL."
undefinedSafe Query Review
安全查询审核
bash
undefinedbash
undefinedGenerate query
生成查询
QUERY=$(gemini -m pro -o text -e "" "Write SQL for: [your request]")
QUERY=$(gemini -m pro -o text -e "" "Write SQL for: [your request]")
Review before executing
执行前审核
echo "Generated query:"
echo "$QUERY"
echo "Generated query:"
echo "$QUERY"
Then execute if safe
确认安全后再执行
psql -c "$QUERY" $DATABASE_URL
psql -c "$QUERY" $DATABASE_URL
undefinedundefinedMigration Patterns
迁移模式
Schema Changes
架构变更
bash
undefinedbash
undefinedCreate migration file
创建迁移文件
cat > migrations/001_add_column.sql << 'EOF'
ALTER TABLE users ADD COLUMN status VARCHAR(50) DEFAULT 'active';
EOF
cat > migrations/001_add_column.sql << 'EOF'
ALTER TABLE users ADD COLUMN status VARCHAR(50) DEFAULT 'active';
EOF
Apply migration
应用迁移
psql -f migrations/001_add_column.sql $DATABASE_URL
undefinedpsql -f migrations/001_add_column.sql $DATABASE_URL
undefinedSafe Migration Workflow
安全迁移流程
bash
undefinedbash
undefined1. Test on copy first
1. 先在副本上测试
createdb test_migration
pg_dump $DATABASE_URL | psql test_migration
createdb test_migration
pg_dump $DATABASE_URL | psql test_migration
2. Run migration on test
2. 在测试环境执行迁移
psql -f migration.sql test_migration
psql -f migration.sql test_migration
3. Verify
3. 验证
psql -c "\d tablename" test_migration
psql -c "\d tablename" test_migration
4. Apply to production
4. 应用到生产环境
psql -f migration.sql $DATABASE_URL
psql -f migration.sql $DATABASE_URL
5. Cleanup
5. 清理
dropdb test_migration
undefineddropdb test_migration
undefinedEnvironment Variables
环境变量
Store connection strings securely:
bash
undefined安全存储连接字符串:
bash
undefined.env file (don't commit!)
.env文件(不要提交到版本库!)
DATABASE_URL=postgresql://user:pass@host:5432/dbname
SQLITE_DB=./data/app.db
DATABASE_URL=postgresql://user:pass@host:5432/dbname
SQLITE_DB=./data/app.db
Usage
使用方式
psql $DATABASE_URL
sqlite3 $SQLITE_DB
undefinedpsql $DATABASE_URL
sqlite3 $SQLITE_DB
undefinedBest Practices
最佳实践
- Never hardcode credentials - Use environment variables
- Review AI-generated queries - Before executing
- Use EXPLAIN - Check query performance
- Test migrations - On copy before production
- Backup before changes - Especially destructive ones
- Use transactions - For multi-statement changes
- Limit results - Always use LIMIT during exploration
- 切勿硬编码凭据 - 使用环境变量
- 审核AI生成的查询 - 执行前务必审核
- 使用EXPLAIN - 检查查询性能
- 测试迁移 - 在生产环境前先在副本上测试
- 变更前备份 - 尤其是破坏性变更
- 使用事务 - 处理多语句变更时
- 限制结果 - 探索数据时始终使用LIMIT