|
# 连接到数据库
sqlite3 D:\wks\xy\fstai\v1\public\db\inspectai_dev.db
# 查看所有数据库(SQLite中称为attached databases)
.databases
# 查看所有表
.tables
# 查看表结构
.schema 表名
# 附加其他数据库文件
ATTACH DATABASE '其他数据库路径.db' AS other_db;
# 切换到其他数据库的表
SELECT * FROM other_db.表名;
|
|
-- 查看当前数据库所有表
SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;
-- 查看表结构
PRAGMA table_info(表名);
-- 查看表索引
PRAGMA index_list(表名);
-- 查看数据库大小
PRAGMA database_size;
-- 附加其他数据库
ATTACH DATABASE 'D:\path\to\other.db' AS db2;
-- 查询其他数据库的表
SELECT * FROM db2.某个表;
-- 分离数据库
DETACH DATABASE db2;
|
|
|
|
|
|
|
|
```
import sqlite3
# 连接主数据库
conn = sqlite3.connect(r'D:\wks\xy\fstai\v1\public\db\inspectai_dev.db')
cursor = conn.cursor()
# 查看所有表
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
print("所有表:", tables)
# 附加其他数据库文件
cursor.execute("ATTACH DATABASE '其他数据库路径.db' AS other_db")
# 查看附加的数据库
cursor.execute("PRAGMA database_list")
databases = cursor.fetchall()
print("所有数据库:", databases)
# 查询其他数据库的表
cursor.execute("SELECT name FROM other_db.sqlite_master WHERE type='table';")
other_tables = cursor.fetchall()
print("其他数据库的表:", other_tables)
# 查询其他数据库的数据
cursor.execute("SELECT * FROM other_db.表名 LIMIT 10")
data = cursor.fetchall()
conn.close()
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|