-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLServer.py
More file actions
38 lines (28 loc) · 1.12 KB
/
MySQLServer.py
File metadata and controls
38 lines (28 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
import mysql.connector
from mysql.connector import Error
def create_database():
try:
# Establish connection to MySQL server
connection = mysql.connector.connect(
host='localhost', # Replace with your MySQL host if different
user='your_username', # Replace with your MySQL username
password='your_password' # Replace with your MySQL password
)
if connection.is_connected():
print("Connected to MySQL Server")
# Create a cursor object
cursor = connection.cursor()
# SQL query to create the database
cursor.execute("CREATE DATABASE IF NOT EXISTS alx_book_store")
print("Database 'alx_book_store' created successfully!")
except mysql.connector.Error as e:
# Catch mysql.connector.Error specifically
print(f"Error: {e}")
finally:
# Close the cursor and connection
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
if __name__ == "__main__":
create_database()