def store_results_to_db(results, db_dir):
# 遍历每个子目录的结果
for common_dir, data in results.items():
# 跳过非字典类型的条目,如unmatched_files_A和unmatched_files_B
if not isinstance(data, dict) or common_dir in ('unmatched_files_A', 'unmatched_files_B', 'high_similarity_pairs', 'low_similarity_pairs'):
continue
# 创建子目录对应的数据库文件
db_path = os.path.join(db_dir, f'{common_dir}.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 创建表
cursor.execute('''CREATE TABLE IF NOT EXISTS unmatched_files_A (
file_name TEXT
)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS unmatched_files_B (
file_name TEXT
)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS high_similarity_pairs (
imgA TEXT,
imgB TEXT,
similarity REAL
)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS low_similarity_pairs (
imgA TEXT,
imgB TEXT,
similarity REAL
)''')
# 插入 unmatched_files_A 和 unmatched_files_B 数据
unmatched_files_A = results.get('unmatched_files_A', {}).get(common_dir, [])
for file_name in unmatched_files_A:
cursor.execute('INSERT INTO unmatched_files_A (file_name) VALUES (?)',
(file_name,))
unmatched_files_B = results.get('unmatched_files_B', {}).get(common_dir, [])
for file_name in unmatched_files_B:
cursor.execute('INSERT INTO unmatched_files_B (file_name) VALUES (?)',
(file_name,))
# 插入 high_similarity_pairs 和 low_similarity_pairs 数据
high_similarity_pairs = results.get('high_similarity_pairs', [])
for imgA, imgB, similarity in high_similarity_pairs:
cursor.execute('INSERT INTO high_similarity_pairs (imgA, imgB, similarity) VALUES (?, ?, ?)',
(imgA, imgB, similarity))
low_similarity_pairs = results.get('low_similarity_pairs', [])
for imgA, imgB, similarity in low_similarity_pairs:
cursor.execute('INSERT INTO low_similarity_pairs (imgA, imgB, similarity) VALUES (?, ?, ?)',
(imgA, imgB, similarity))
conn.commit()
conn.close()
# 使用函数
dirA = r'D:/Code/PDF_to_img/PDFCORE_PDF_IMG_BACKGROUND'
dirB = r'D:/Code/PDF_to_img/LIBHARU_PDF_IMG_BACKGROUND'
pixel_threshold = 50
sector_threshold = 5
sector=72
similarity_threshold = 70
not_similarity_threshold = 30
db_dir= r"Db_DIR"
# 使用函数
results = compare_directories(dirA, dirB, pixel_threshold, sector_threshold,similarity_threshold,not_similarity_threshold)
store_results_to_db(results, db_dir)
标签:files,pairs,similarity,处理,数据库,results,unmatched,cursor
From: https://www.cnblogs.com/DINGJINXING/p/18294940