In [1]:
import pymysql
In [14]:
db = pymysql.connect("localhost","root","None" ,database="schooldb")
cursor = db.cursor()
In [16]:
cursor.execute('show tables;')
cursor.fetchall()
Out[16]:
In [21]:
query = """
SELECT course.name FROM student
INNER JOIN enrolls_in ON student.ssn = enrolls_in.ssn
INNER JOIN course ON course.number = enrolls_in.class
WHERE f_name = "JOHN";
"""
cursor.execute(query)
cursor.fetchall()
Out[21]:
In [ ]:
# 向数据库插入数据
insert = 'INSERT INTO Student VALUES ("", "")'
cursor.execute(insert)
cursor.fetchall()
In [ ]:
# 同步到数据库
db.commit()