Commit a70c9250 by chenxi

提交python脚本

1 parent 3dfeb115
import hashlib
import pymysql
import requests
# 数据库连接配置
db_config = {
'host': '192.168.5.1',
'port': 3307,
'user': 'user_yangll',
'password': 'vGxw9jWg',
'db': 'glsx_car_service',
'charset': 'utf8mb4',
'cursorclass': pymysql.cursors.DictCursor
}
def main():
# 建立数据库连接
connection = pymysql.connect(**db_config)
try:
# 创建游标对象
with connection.cursor() as cursor:
# 查询所有图片路径
sql_select = "SELECT id, file_path FROM sys_order_file WHERE file_md5 = ''"
cursor.execute(sql_select)
results = cursor.fetchall()
# 遍历结果,下载图片并计算MD5
for row in results:
file_id = row['id']
file_path = row['file_path']
try:
# 获取图片内容
print('file_path:' + file_path)
response = requests.get(file_path)
response.raise_for_status() # 确保请求成功
image_content = response.content
# 计算MD5值
md5_hash = hashlib.md5()
md5_hash.update(image_content)
file_md5 = md5_hash.hexdigest()
print('file_md5' + file_md5)
# 更新数据库中的file_md5字段
sql_update = "UPDATE sys_order_file SET file_md5 = %s WHERE id = %s"
cursor.execute(sql_update, (file_md5, file_id))
except requests.RequestException as e:
print(f"无法获取图片或计算MD5: {file_path}, 错误: {e}")
# 提交更改
connection.commit()
finally:
# 关闭数据库连接
connection.close()
if __name__ == "__main__":
main()
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!