mysql_char_find.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import pymysql
  4. # MySQL连接配置
  5. host = '47.98.212.223'
  6. user = '6636'
  7. password = 'Mt7DiMvk7W_2'
  8. database = 'db-tm'
  9. # 要查找的字符串
  10. search_string = 'timichat.net'
  11. # 要写入的txt文件路径
  12. output_file = 'output10.txt'
  13. # 跟踪已写入的字符串
  14. written_strings = set()
  15. # 连接到MySQL数据库
  16. connection = pymysql.connect(
  17. host=host, user=user, password=password, database=database)
  18. try:
  19. with connection.cursor() as cursor:
  20. # 获取所有表名
  21. cursor.execute("SHOW TABLES")
  22. tables = cursor.fetchall()
  23. # 遍历每个表
  24. for table in tables:
  25. table_name = table[0]
  26. # if table_name == "tb_release_config":
  27. print("123")
  28. # 查询表中所有字段
  29. cursor.execute(f"SELECT * FROM `{table_name}`")
  30. rows = cursor.fetchall()
  31. # 遍历每一行数据
  32. for row in rows:
  33. for value in row:
  34. if isinstance(value, str) and search_string in value and len(value) < 150000 and value not in written_strings:
  35. # 写入符合条件的字符串到txt文件
  36. with open(output_file, 'a') as file:
  37. file.write(value + '\n')
  38. # 将写入的字符串添加到集合中
  39. written_strings.add(value)
  40. print("查询完成并写入到txt文件。")
  41. finally:
  42. connection.close()