-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.py
31 lines (27 loc) · 824 Bytes
/
connect.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
import mysql.connector
dbuser = 'root'
dbpass = 'root'
dbhost = 'localhost' # or the hostname where your MySQL server is running
dbname = 'motorkhana'
def create_db_connection():
try:
connection = mysql.connector.connect(
user=dbuser,
password=dbpass,
host=dbhost,
database=dbname,
autocommit=True
)
return connection
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
def close_db_connection(connection):
if connection:
connection.close()
if __name__ == "__main__":
# This code is for testing the database connection
connection = create_db_connection()
if connection:
print("Connected to the database successfully.")
close_db_connection(connection)