-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_utils1.py
49 lines (38 loc) · 1.36 KB
/
mysql_utils1.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
"""
Connects to MySQL server and prints tables in 'academicworld' database.
"""
import mysql.connector
def connect_to_mysql():
# password = input("Enter MySQL password for root user: ")
password = "test_root" # User: enter your password here once
try:
connection = mysql.connector.connect(
user="root",
password=password,
database="academicworld"
)
if connection.is_connected():
# print("Connected to MySQL server")
cursor = connection.cursor()
# List tables
cursor.execute("SHOW TABLES")
tables = cursor.fetchall()
# if tables:
# # print("we got tables")
# # print("Tables in 'academicworld' database:")
# # for table in tables:
# # print(table[0])
# else:
# print("No tables found in 'academicworld' database.")
return connection
except mysql.connector.Error as error:
print("Error connecting to MySQL server:", error)
return None
finally:
if 'connection' in locals() and connection.is_connected():
cursor.close()
return connection
# connection.close()
# print("MySQL connection closed")
if __name__ == "__main__":
connect_to_mysql()