-
Notifications
You must be signed in to change notification settings - Fork 8
/
mysql_functions.py
128 lines (120 loc) · 4.72 KB
/
mysql_functions.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import mysql.connector
from mysql.connector import Error # import Error function separately to have easy access to it
# ------------------------- Connecting to MySQL Server to get Access to all DBs
def create_server_connection(host_name, port_name, user_name, user_password): # establish a connection to that server.
connection = None
try:
connection = mysql.connector.connect(
host=host_name,
port = port_name,
user=user_name,
passwd=user_password
)
print("MySQL Database connection successful")
return connection
except Error as err:
print(f"Error: '{err}'")
return None
# ------------------------- Show Available Databases based on the connection you established
def show_databases(connection):
mycursor = connection.cursor(buffered=True)
list_of_dbs = []
try:
mycursor.execute("SHOW DATABASES")
for database_tuple in mycursor:
list_of_dbs.append(database_tuple[0])
except Error as err:
print(f"Error: '{err}'")
return list_of_dbs
# ------------------------- Check is there is a particular Database based on the connection you established
def check_database(connection,db_name_check):
mycursor = connection.cursor(buffered=True)
try:
mycursor.execute("SHOW DATABASES")
for database_tuple in mycursor:
database_name = database_tuple[0]
if(database_name == db_name_check):
print("We have this Database")
break
else:
print("No such a Database")
except Error as err:
print(f"Error: '{err}'")
return None
# ------------------------- Create a Database and Check if already exists based on the connection you established
def create_database(connection,new_db_name):
mycursor = connection.cursor(buffered=True)
try:
mycursor.execute("SHOW DATABASES")
for database_tuple in mycursor:
database_name = database_tuple[0]
if(database_name == new_db_name):
print("We have this Database")
break
else:
try:
mycursor.execute("CREATE DATABASE "+new_db_name)
print("Database created successfully")
except Error as err:
print(f"Error: '{err}'")
except Error as err:
print(f"Error: '{err}'")
return None
# ------------------------- Delete a Database and Check if exists based on the connection you established
def delete_database(connection,db_name):
mycursor = connection.cursor(buffered=True)
try:
mycursor.execute("SHOW DATABASES")
for database_tuple in mycursor:
database_name = database_tuple[0]
if(database_name == db_name):
try:
mycursor.execute("DROP DATABASE "+db_name)
print("Database deleted successfully")
except Error as err:
print(f"Error: '{err}'")
else:
print("No such a Database")
except Error as err:
print(f"Error: '{err}'")
return None
# ------------------------- Connecting to MySQL Server in a particular Database
def db_connection(host_name, port_name, user_name, user_password, db_name): # establish a connection to that server.
create_server_connection(host_name, port_name, user_name, user_password)
connection = None
try:
connection = mysql.connector.connect(
host=host_name,
port = port_name,
user=user_name,
passwd=user_password,
database=db_name # <---- the only difference is here to the create_server_connection function
)
print("Connection successful to "+db_name)
return connection
except Error as err:
print(f"Error: '{err}'")
return None
# ------------------------- Query Execution Function for Feching
# You must fetch all rows for the current query before executing new statements using the same connection.
def execute_query_fetch(connection, query):
mycursor = connection.cursor()
try:
mycursor.execute(query)
myresult = mycursor.fetchall()
return(myresult)
print("Query successful")
except Error as err:
print(f"Error: '{err}'")
return None
# ------------------------- Query Execution Function for Commiting
# The commit() method is one among the various methods in Python which is used to make the database transactions.
def execute_query_commit(connection, query):
mycursor = connection.cursor()
try:
mycursor.execute(query)
connection.commit()
print("Query successful")
except Error as err:
print(f"Error: '{err}'")
return None