MobileModels
MobileModels copied to clipboard
提供写入数据的sql语句
非常感谢提供详尽的数据。 我写了一段python代码,可以提取目录中型号以及手机名称对应关系,生成sql语句到数据库中,方便大家使用。
import os
# 定义目录路径
directory_path = 'D:\\project\\github\\MobileModels\\brands' # 替换成你的md文件目录路径
output_file = os.path.join(directory_path, 'insert.sql')
# 存储匹配结果
matches = []
# 打开日志文件
log_file_path = os.path.join(directory_path, 'processing.log')
with open(log_file_path, 'w', encoding='utf-8') as log_file:
# 遍历目录中的所有md文件
for filename in os.listdir(directory_path):
if filename.endswith('.md'):
log_file.write(f'Processing file: {filename}\n')
print(f'Processing file: {filename}')
with open(os.path.join(directory_path, filename), 'r', encoding='utf-8') as file:
for line in file:
line = line.strip()
if line.startswith('`') and ':' in line:
parts = line.split(':')
user_agent_device_id = parts[0].strip('`')
user_agent_device_name = ':'.join(parts[1:]).strip()
matches.append((user_agent_device_id, user_agent_device_name))
log_file.write(f'Matched: {user_agent_device_id}, {user_agent_device_name}\n')
# 生成SQL插入语句
with open(output_file, 'w', encoding='utf-8') as file:
file.write('USE your_database_name;\n') # 替换成你的数据库名称
file.write('INSERT INTO user_agent_device_model (user_agent_device_id, user_agent_device_name) VALUES\n')
values = []
for match in matches:
user_agent_device_id, user_agent_device_name = match
values.append(f"('{user_agent_device_id}', '{user_agent_device_name}')")
file.write(",\n".join(values) + ";\n")
print(f'SQL insert statements have been written to {output_file}')
print(f'Processing log has been written to {log_file_path}')