-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrel_db.py
More file actions
44 lines (36 loc) · 1.13 KB
/
rel_db.py
File metadata and controls
44 lines (36 loc) · 1.13 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
37
38
39
40
41
42
43
44
import pymysql
def create_mysql_connection():
"""
This function creates a MySQL database connection.
Returns:
The connection to the MySQL database.
"""
connection = pymysql.connect(
host='localhost',
user='username',
password='password',
database='mydatabase'
)
return connection
def insert_planet(connection, name, radius, mass):
"""
This function inserts a planet into the MySQL database.
Args:
connection: The connection to the MySQL database.
name: The name of the planet.
radius: The radius of the planet.
mass: The mass of the planet.
Returns:
None.
"""
cursor = connection.cursor()
sql = "INSERT INTO planets (name, radius, mass) VALUES (%s, %s, %s)"
values = (name, radius, mass)
cursor.execute(sql, values)
connection.commit()
if __name__ == "__main__":
connection = create_mysql_connection()
insert_planet(connection, "Earth", 6371, 5.972e24)
insert_planet(connection, "Mars", 3389, 6.421e23)
insert_planet(connection, "Jupiter", 69911, 1.898e27)
connection.close()