Files
fastcopy/db_manage/create_tables.py
Burgess Leo e167ff5d9f temp store
2025-05-19 10:21:12 +08:00

379 lines
10 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import sqlite3
def CreateDBConfigTable(db_path='../src/db_ntfs_info.db', table_name='db_config'):
"""
在指定路径下创建 SQLite 数据库,并在其中创建配置表。
:param db_path: str, 数据库文件的路径
:param table_name: str, 要创建的表名
:return: None
"""
# 确保目录存在
directory = os.path.dirname(db_path)
if directory and not os.path.exists(directory):
os.makedirs(directory)
# 连接到SQLite数据库如果文件不存在会自动创建
conn = sqlite3.connect(db_path)
# 创建一个游标对象
cursor = conn.cursor()
# 动态构建创建表的SQL语句
create_table_sql = f"""
CREATE TABLE IF NOT EXISTS {table_name} (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
Key TEXT UNIQUE NOT NULL,
Value TEXT NOT NULL
);
"""
# 执行SQL语句
cursor.execute(create_table_sql)
# 提交更改
conn.commit()
# 关闭连接
conn.close()
print(f"表 [{table_name}] 已在数据库 [{db_path}] 中创建成功")
def CreateDBDeviceTable(db_path='../src/db_ntfs_info.db', table_name='db_device'):
"""
在指定路径下创建 SQLite 数据库,并在其中创建设备信息表。
:param db_path: str, 数据库文件的路径
:param table_name: str, 要创建的表名
:return: None
"""
# 确保目录存在
directory = os.path.dirname(db_path)
if directory and not os.path.exists(directory):
os.makedirs(directory)
# 连接到SQLite数据库如果文件不存在会自动创建
conn = sqlite3.connect(db_path)
# 创建一个游标对象
cursor = conn.cursor()
# 动态构建创建表的SQL语句
create_table_sql = f"""
CREATE TABLE IF NOT EXISTS {table_name} (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
Path TEXT NOT NULL UNIQUE, -- 添加 UNIQUE 约束
Type TEXT NOT NULL CHECK(Type IN ('磁盘', '文件', '文件夹')),
Option TEXT
);
"""
# 执行SQL语句
cursor.execute(create_table_sql)
# 提交更改
conn.commit()
# 关闭连接
conn.close()
print(f"表 [{table_name}] 已在数据库 [{db_path}] 中创建成功")
def CreateDBNodeTable(db_path='../src/db_ntfs_info.db', table_name='db_node'):
"""
在指定路径下创建 SQLite 数据库,并在其中创建节点信息表。
:param db_path: str, 数据库文件的路径
:param table_name: str, 要创建的表名
:return: None
"""
# 确保目录存在
directory = os.path.dirname(db_path)
if directory and not os.path.exists(directory):
os.makedirs(directory)
# 连接到SQLite数据库如果文件不存在会自动创建
conn = sqlite3.connect(db_path)
# 创建一个游标对象
cursor = conn.cursor()
# 动态构建创建表的SQL语句
create_table_sql = f"""
CREATE TABLE IF NOT EXISTS {table_name} (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
PathID INTEGER,
ParentID INTEGER,
NameHash TEXT,
PathHash TEXT,
ExtendNameID INTEGER,
DirLayer INTEGER,
GroupID INTEGER,
UserID INTEGER,
FileCreateTime TEXT,
FileModifyTime TEXT,
FileAccessTime TEXT,
FileAuthTime TEXT,
FileSize INTEGER,
FileMode INTEGER,
FileHash TEXT,
ExtentCount INTEGER,
extent1_DeviceID INTEGER,
extent1_Location INTEGER,
extent1_Length INTEGER,
extent2_DeviceID INTEGER,
extent2_Location INTEGER,
extent2_Length INTEGER,
extent3_DeviceID INTEGER,
extent3_Location INTEGER,
extent3_Length INTEGER,
extent4_DeviceID INTEGER,
extent4_Location INTEGER,
extent4_Length INTEGER,
-- 外键约束(可选)
FOREIGN KEY(PathID) REFERENCES db_path(ID),
FOREIGN KEY(ExtendNameID) REFERENCES extname_table(ID),
FOREIGN KEY(GroupID) REFERENCES groups(ID),
FOREIGN KEY(UserID) REFERENCES users(ID)
);
"""
# 执行SQL语句
cursor.execute(create_table_sql)
# 提交更改
conn.commit()
# 关闭连接
conn.close()
print(f"表 [{table_name}] 已在数据库 [{db_path}] 中创建成功")
def CreateDBUserTable(db_path='../src/db_ntfs_info.db', table_name='db_user'):
"""
在指定路径下创建 SQLite 数据库,并在其中创建用户表。
:param db_path: str, 数据库文件的路径
:param table_name: str, 要创建的表名
:return: None
"""
# 确保目录存在
directory = os.path.dirname(db_path)
if directory and not os.path.exists(directory):
os.makedirs(directory)
# 连接到SQLite数据库如果文件不存在会自动创建
conn = sqlite3.connect(db_path)
# 创建一个游标对象
cursor = conn.cursor()
# 动态构建创建表的SQL语句
create_table_sql = f"""
CREATE TABLE IF NOT EXISTS {table_name} (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
UserName TEXT UNIQUE NOT NULL
);
"""
# 执行SQL语句
cursor.execute(create_table_sql)
# 提交更改
conn.commit()
# 关闭连接
conn.close()
print(f"表 [{table_name}] 已在数据库 [{db_path}] 中创建成功")
def CreateDBGroupTable(db_path='../src/db_ntfs_info.db', table_name='db_group'):
"""
在指定路径下创建 SQLite 数据库,并在其中创建组表。
:param db_path: str, 数据库文件的路径
:param table_name: str, 要创建的表名
:return: None
"""
# 确保目录存在
directory = os.path.dirname(db_path)
if directory and not os.path.exists(directory):
os.makedirs(directory)
# 连接到SQLite数据库如果文件不存在会自动创建
conn = sqlite3.connect(db_path)
# 创建一个游标对象
cursor = conn.cursor()
# 动态构建创建表的SQL语句
create_table_sql = f"""
CREATE TABLE IF NOT EXISTS {table_name} (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
GroupName TEXT UNIQUE NOT NULL
);
"""
# 执行SQL语句
cursor.execute(create_table_sql)
# 提交更改
conn.commit()
# 关闭连接
conn.close()
print(f"表 [{table_name}] 已在数据库 [{db_path}] 中创建成功")
def CreateDBExtendSnippetTable(db_path='../src/db_ntfs_info.db', table_name='db_extend_extent'):
"""
在指定路径下创建 SQLite 数据库,并在其中创建扩展片段表。
:param db_path: str, 数据库文件的路径
:param table_name: str, 要创建的表名
:return: None
"""
# 确保目录存在
directory = os.path.dirname(db_path)
if directory and not os.path.exists(directory):
os.makedirs(directory)
# 连接到SQLite数据库如果文件不存在会自动创建
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 动态构建创建表的SQL语句
create_table_sql = f"""
CREATE TABLE IF NOT EXISTS {table_name} (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
NodeID INTEGER NOT NULL,
ExtentNumber INTEGER NOT NULL,
extent_DeviceID INTEGER,
extent_Location INTEGER,
extent_Length INTEGER,
-- 外键约束(可选)
FOREIGN KEY(NodeID) REFERENCES db_node(ID)
);
"""
# 执行SQL语句
cursor.execute(create_table_sql)
# 提交更改
conn.commit()
conn.close()
print(f"表 [{table_name}] 已在数据库 [{db_path}] 中创建成功")
def CreateDBPathTable(db_path='../src/db_path.db', table_name='db_path'):
"""
在指定路径下创建 SQLite 数据库,并在其中创建路径信息表,
包含 DeviceID 字段,用于标记文件所属设备(磁盘)。
:param db_path: str, 数据库文件的路径
:param table_name: str, 要创建的表名
:return: None
"""
# 确保目录存在
directory = os.path.dirname(db_path)
if directory and not os.path.exists(directory):
os.makedirs(directory)
# 连接到SQLite数据库如果文件不存在会自动创建
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 动态构建创建表的SQL语句包含 DeviceID 外键)
create_table_sql = f"""
CREATE TABLE IF NOT EXISTS {table_name} (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
-- DeviceID TEXT NOT NULL,
Path TEXT NOT NULL,
Name TEXT NOT NULL,
PathHash TEXT UNIQUE NOT NULL,
IsDir INTEGER NOT NULL CHECK(IsDir IN (0, 1)),
ParentID INTEGER,
ContentSize INTEGER,
-- 外键约束
-- FOREIGN KEY(DeviceID) REFERENCES db_device(ID),
FOREIGN KEY(ParentID) REFERENCES {table_name}(ID)
);
"""
# 执行SQL语句
cursor.execute(create_table_sql)
# 提交更改
conn.commit()
conn.close()
print(f"表 [{table_name}] 已在数据库 [{db_path}] 中创建成功")
def CreateDBExtendNameTable(db_path='../src/db_extend_name.db', table_name='db_extend_name'):
"""
在指定路径下创建 SQLite 数据库,并在其中创建扩展名表。
:param db_path: str, 数据库文件的路径
:param table_name: str, 要创建的表名
:return: None
"""
# 确保目录存在
directory = os.path.dirname(db_path)
if directory and not os.path.exists(directory):
os.makedirs(directory)
# 连接到SQLite数据库如果文件不存在会自动创建
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 动态构建创建表的SQL语句
create_table_sql = f"""
CREATE TABLE IF NOT EXISTS {table_name} (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
ExtendName TEXT UNIQUE NOT NULL
);
"""
# 执行SQL语句
cursor.execute(create_table_sql)
# 提交更改
conn.commit()
conn.close()
print(f"表 [{table_name}] 已在数据库 [{db_path}] 中创建成功")
def main():
CreateDBDeviceTable(db_path='../src/db_ntfs_info.db', table_name='db_device')
CreateDBConfigTable(db_path='../src/db_ntfs_info.db', table_name='db_config')
CreateDBNodeTable(db_path='../src/db_ntfs_info.db', table_name='db_node')
CreateDBUserTable(db_path='../src/db_ntfs_info.db', table_name='db_user')
CreateDBGroupTable(db_path='../src/db_ntfs_info.db', table_name='db_group')
CreateDBExtendSnippetTable(db_path='../src/db_ntfs_info.db', table_name='db_extend_extent')
CreateDBPathTable(db_path='../src/db_ntfs_info.db', table_name='db_path')
CreateDBExtendNameTable(db_path='../src/db_ntfs_info.db', table_name='db_extend_name')
if __name__ == '__main__':
main()