-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
39 lines (35 loc) · 1.12 KB
/
Copy pathdatabase.py
File metadata and controls
39 lines (35 loc) · 1.12 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
import mysql.connector
from mysql.connector import Error
# connect to mySQL database
def create_connection(host_name, user_name, user_password, db_name):
connection = None
try:
connection = mysql.connector.connect(
host=host_name,
user=user_name,
passwd=user_password,
database=db_name
)
print(f"Connection to MySQL DB successful, using '{db_name}' database")
except Error as e:
print(f"The error '{e}' occurred")
return connection
# executing MySQL queries
def execute_query(connection, query):
cursor = connection.cursor()
try:
cursor.execute(query)
connection.commit()
print("Query executed successfully")
except Error as e:
print(f"The error '{e}' occurred")
def execute_read_query(connection, query):
cursor = connection.cursor()
result = None
try:
cursor.execute(query)
result = cursor.fetchall()
print("Query executed successfully")
return result
except Error as e:
print(f"The error '{e}' occurred")