-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_operations.py
More file actions
68 lines (62 loc) · 2.31 KB
/
db_operations.py
File metadata and controls
68 lines (62 loc) · 2.31 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
64
65
66
67
68
import mysql.connector
from mysql.connector import Error
import logging
# Set up logging configuration
logging.basicConfig(
filename='db_operations.log',
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def create_connection():
"""Create a database connection."""
try:
connection = mysql.connector.connect(
host='host_name', # MySQL host
user='your_username', # MySQL username
password='your_password', # MySQL password
database='ip_addresses', # MySQL database
port='your_port_number' # MySQL Port Number
)
if connection.is_connected():
logging.info("Connected to MySQL database")
return connection
except Error as e:
logging.error(f"Error connecting to MySQL database: {e}")
return None
def create_table(connection):
"""Create a table to store IP addresses."""
try:
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS ip_addresses (
id INT AUTO_INCREMENT PRIMARY KEY,
ip_addresses VARCHAR(45) NOT NULL
)
""")
connection.commit()
logging.info("Table created successfully")
except Error as e:
logging.error(f"Error creating table: {e}")
def insert_ip_address(connection, ip_address):
"""Insert an IP address into the table."""
try:
cursor = connection.cursor()
cursor.execute("INSERT INTO ip_addresses (ip_address) VALUES (%s)", (ip_address,))
connection.commit()
logging.info(f"IP Address {ip_address} inserted successfully")
except Error as e:
logging.error(f"Error inserting IP Address {ip_address}: {e}")
def main():
"""Main function to execute the script."""
connection = create_connection()
if connection:
try:
create_table(connection)
ip_address = '192.168.1.1' # Example IP address
insert_ip_address(connection, ip_address)
finally:
if connection.is_connected():
connection.close()
logging.info("Database connection closed")
if __name__ == "__main__":
main()