-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb_connect.py
More file actions
63 lines (57 loc) · 1.75 KB
/
Copy pathdb_connect.py
File metadata and controls
63 lines (57 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import mysql.connector
import logging
from dotenv import load_dotenv
import os
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv('../config/.env')
def get_connection():
"""Create and return MySQL database connection"""
try:
return mysql.connector.connect(
host=os.getenv("DB_HOST", "localhost"),
user=os.getenv("DB_USER", "root"),
password=os.getenv("DB_PASSWORD", "Timmy@2013"),
database=os.getenv("DB_NAME", "job_scraper")
)
except mysql.connector.Error as err:
logger.error(f"Database connection failed: {err}")
raise
def insert_job(data):
"""Insert job data into MySQL database"""
conn = None
try:
conn = get_connection()
cursor = conn.cursor()
sql = """
INSERT INTO jobs (
title,
company,
location,
link,
source,
date_posted,
work_type,
employment_type,
description
)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
title=VALUES(title),
company=VALUES(company),
location=VALUES(location),
work_type=VALUES(work_type),
employment_type=VALUES(employment_type),
description=VALUES(description)
"""
cursor.execute(sql, data)
conn.commit()
logger.info(f"Inserted job: {data[0]} at {data[1]}")
except mysql.connector.Error as err:
logger.error(f"Failed to insert job: {err}")
finally:
if conn and conn.is_connected():
cursor.close()
conn.close()