-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10th.py
38 lines (29 loc) · 1.11 KB
/
10th.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
import mysql.connector
# Define database connection details
config = {
'host': 'localhost',
'user': 'your_username',
'password': 'your_password',
'database': 'example_db'
}
# Connect to the database
conn = mysql.connector.connect(**config)
# Create a cursor object to execute SQL queries
c = conn.cursor()
# Create a table
c.execute('''CREATE TABLE IF NOT EXISTS students
(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), age INT, email VARCHAR(255))''')
# Insert data into the table
c.execute("INSERT INTO students (name, age, email) VALUES (%s, %s, %s)", ("John", 25, "john@example.com"))
c.execute("INSERT INTO students (name, age, email) VALUES (%s, %s, %s)", ("Jane", 30, "jane@example.com"))
c.execute("INSERT INTO students (name, age, email) VALUES (%s, %s, %s)", ("Minhaj", 27, "shaikhminhaj.dev@gmail.com"))
# Commit changes to the database
conn.commit()
# Query the table and fetch results
c.execute("SELECT * FROM students")
rows = c.fetchall()
# Print the results
for row in rows:
print(row)
# Close the database connection
conn.close()