-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconnect.py
More file actions
29 lines (22 loc) · 721 Bytes
/
connect.py
File metadata and controls
29 lines (22 loc) · 721 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
import mysql.connector
# Establish a connection to the MySQL database
connection = mysql.connector.connect(
host="localhost",
user="chart_user",
password="password",
database="charts"
)
# Check if the connection was successful
if connection.is_connected():
print("Connected to MySQL database")
# Create a cursor object
cursor = connection.cursor()
# Create a table with username, first_name and last_name columns
cursor.execute("CREATE TABLE users (username VARCHAR(50), first_name VARCHAR(50), last_name VARCHAR(50))")
# Close the cursor
cursor.close()
else:
print("Connection failed")
# Perform database operations here...
# Close the connection
connection.close()