restore mft_analyze
This commit is contained in:
@@ -22,9 +22,13 @@ def GetExtendNameId(name: str, cursor: sqlite3.Cursor) -> int:
|
||||
|
||||
# 获取 DirLayer(路径层级)
|
||||
def GetDirLayer(path: str) -> int:
|
||||
# 示例:Z:\demo.jpg → 层级为0;Z:\pictures\RHCE.jpg → 层级为1
|
||||
path = path.strip().strip("\\")
|
||||
return path.count("\\")
|
||||
# "Z:\demo.jpg" → 0 (根目录文件)
|
||||
# "Z:\pictures\RHCE.jpg" → 1 (一级子目录)
|
||||
path = path.strip()
|
||||
if not path or path == "\\":
|
||||
return 0
|
||||
# 计算路径中的反斜杠数量,减去根目录的反斜杠
|
||||
return path.count("\\") - 1
|
||||
|
||||
|
||||
# 获取 GroupID(默认第一个)
|
||||
@@ -74,7 +78,7 @@ def GetRandomLength() -> int:
|
||||
|
||||
|
||||
# 主函数:将 db_path 数据导入 db_node
|
||||
def MigratePathToNode(db_path='../src/db_ntfs_info.db'):
|
||||
def InsertNodeDataToDB(db_path='../src/db_ntfs_info.db', table_name='db_node'):
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
@@ -85,9 +89,18 @@ def MigratePathToNode(db_path='../src/db_ntfs_info.db'):
|
||||
cursor.execute("SELECT ID, Path, Name, ParentID FROM db_path")
|
||||
rows = cursor.fetchall()
|
||||
|
||||
inserted_count = 0 # 新增:记录实际插入的条目数
|
||||
|
||||
for row in rows:
|
||||
path_id, full_path, name, parent_id = row
|
||||
|
||||
# 检查是否已存在相同 PathID
|
||||
cursor.execute("SELECT COUNT(*) FROM db_node WHERE PathID = ?", (path_id,))
|
||||
exists = cursor.fetchone()[0]
|
||||
if exists > 0:
|
||||
print(f"⚠️ PathID {path_id} 已存在,跳过插入")
|
||||
continue
|
||||
|
||||
# 计算字段
|
||||
name_hash = hashlib.sha256(name.encode()).hexdigest()
|
||||
dir_layer = GetDirLayer(full_path)
|
||||
@@ -136,15 +149,21 @@ def MigratePathToNode(db_path='../src/db_ntfs_info.db'):
|
||||
|
||||
# 构建 SQL 插入语句
|
||||
placeholders = ', '.join('?' * len(values))
|
||||
insert_sql = f"INSERT INTO db_node ({', '.join(fields)}) VALUES ({placeholders})"
|
||||
insert_sql = f"INSERT INTO {table_name} ({', '.join(fields)}) VALUES ({placeholders})"
|
||||
|
||||
# 执行插入
|
||||
cursor.execute(insert_sql, values)
|
||||
inserted_count += 1 # 新增:成功插入后计数器加1
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print("✅ db_path 数据已成功迁移到 db_node 表")
|
||||
|
||||
# 新增:根据插入结果输出不同信息
|
||||
if inserted_count > 0:
|
||||
print(f"✅ 成功插入 {inserted_count} 条数据到 {table_name} 表")
|
||||
else:
|
||||
print("ℹ️ 没有新的数据被插入数据库(可能所有条目已存在或没有可处理的数据)")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
MigratePathToNode()
|
||||
InsertNodeDataToDB()
|
Reference in New Issue
Block a user