-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
29 lines (27 loc) · 920 Bytes
/
Copy pathconfig.py
File metadata and controls
29 lines (27 loc) · 920 Bytes
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
# Connect with Databases
import contextlib
import mysql.connector
mysql_config = {
'host': 'localhost',
'user': 'root',
'password': '',
'database': 'testpy'
}
def create_database():
with contextlib.suppress(mysql.connector.Error):
connection = mysql.connector.connect(
host=mysql_config['host'],
user=mysql_config['user'],
password=mysql_config['password']
)
cursor = connection.cursor()
cursor.execute(f"CREATE DATABASE IF NOT EXISTS {mysql_config['database']}")
connection.close()
try:
connection = mysql.connector.connect(**mysql_config)
print(f"Database '{mysql_config['database']}' Successfully Created Or Already to Use!")
connection.close()
except mysql.connector.Error as e:
print(f"Failed to connect with Databases! : {e}")
if __name__ == "__main__":
create_database()