mayankjoin3

check sql keywords

Apr 23rd, 2026
106
0
Never
5
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. import os
  2. import re
  3. import csv
  4. from datetime import datetime
  5.  
  6. ROOT_DIR = r"C:\Users\Mayank\Documents\GitHub\ERP\hostel"
  7.  
  8. # SQL keywords to scan
  9. SQL_KEYWORDS = [
  10.     "SELECT", "INSERT", "UPDATE", "DELETE",
  11.     "CREATE", "DROP", "ALTER", "TRUNCATE",
  12.     "REPLACE", "GRANT", "REVOKE"
  13. ]
  14.  
  15. # Compile regex (case-insensitive)
  16. pattern = re.compile(r"\b(" + "|".join(SQL_KEYWORDS) + r")\b", re.IGNORECASE)
  17.  
  18. # Output file
  19. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  20. output_file = f"sql_keyword_scan_{timestamp}.csv"
  21.  
  22. results = []
  23.  
  24. # Walk through directory
  25. for root, _, files in os.walk(ROOT_DIR):
  26.     for file in files:
  27.         if file.lower().endswith(".php"):
  28.             file_path = os.path.join(root, file)
  29.  
  30.             try:
  31.                 with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
  32.                     for lineno, line in enumerate(f, start=1):
  33.                         matches = pattern.findall(line)
  34.                         if matches:
  35.                             for m in matches:
  36.                                 results.append([
  37.                                     file_path.strip(),
  38.                                     lineno,
  39.                                     m.upper(),
  40.                                     line.strip()
  41.                                 ])
  42.             except Exception as e:
  43.                 print(f"Error reading {file_path}: {e}")
  44.  
  45. # Write CSV
  46. with open(output_file, "w", newline="", encoding="utf-8") as csvfile:
  47.     writer = csv.writer(csvfile)
  48.     writer.writerow(["file_path", "line_number", "keyword", "line"])
  49.     writer.writerows(results)
  50.  
  51. print(f"[DONE] Output saved to {output_file}")
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment