Update sqlite3-python/README.md
This commit is contained in:
@@ -20,14 +20,14 @@ class SQL:
|
|||||||
```
|
```
|
||||||
|
|
||||||
```python
|
```python
|
||||||
#
|
#%%
|
||||||
try: get_ipython()
|
try: get_ipython()
|
||||||
except: display = print
|
except: display = print
|
||||||
|
|
||||||
sql = SQL()
|
sql = SQL()
|
||||||
sql
|
sql
|
||||||
|
|
||||||
#
|
#%%
|
||||||
# 학생 테이블 생성
|
# 학생 테이블 생성
|
||||||
sql('''CREATE TABLE IF NOT EXISTS students
|
sql('''CREATE TABLE IF NOT EXISTS students
|
||||||
(id INTEGER PRIMARY KEY, name TEXT, age INTEGER, grade INTEGER)''')
|
(id INTEGER PRIMARY KEY, name TEXT, age INTEGER, grade INTEGER)''')
|
||||||
@@ -43,7 +43,7 @@ sql('''CREATE TABLE IF NOT EXISTS enrollments
|
|||||||
FOREIGN KEY(course_id) REFERENCES courses(id))''')
|
FOREIGN KEY(course_id) REFERENCES courses(id))''')
|
||||||
sql
|
sql
|
||||||
|
|
||||||
#
|
#%%
|
||||||
# 학생 데이터 삽입
|
# 학생 데이터 삽입
|
||||||
sql('INSERT INTO students (name, age, grade) VALUES (?, ?, ?)', ('김철수', 20, 2))
|
sql('INSERT INTO students (name, age, grade) VALUES (?, ?, ?)', ('김철수', 20, 2))
|
||||||
sql('INSERT INTO students (name, age, grade) VALUES (?, ?, ?)', ('이영희', 22, 4))
|
sql('INSERT INTO students (name, age, grade) VALUES (?, ?, ?)', ('이영희', 22, 4))
|
||||||
@@ -67,7 +67,7 @@ display(sql.courses)
|
|||||||
print("\nEnrollments:")
|
print("\nEnrollments:")
|
||||||
display(sql.enrollments)
|
display(sql.enrollments)
|
||||||
|
|
||||||
#
|
#%%
|
||||||
# JOIN을 사용하여 학생별 수강 과목 조회
|
# JOIN을 사용하여 학생별 수강 과목 조회
|
||||||
print("\n학생별 수강 과목:")
|
print("\n학생별 수강 과목:")
|
||||||
enrollments_df = sql('''
|
enrollments_df = sql('''
|
||||||
@@ -78,7 +78,7 @@ enrollments_df = sql('''
|
|||||||
''')
|
''')
|
||||||
display(enrollments_df)
|
display(enrollments_df)
|
||||||
|
|
||||||
#
|
#%%
|
||||||
# 특정 과목을 수강하는 학생 수 조회
|
# 특정 과목을 수강하는 학생 수 조회
|
||||||
course_name = '알고리즘'
|
course_name = '알고리즘'
|
||||||
result = sql('''
|
result = sql('''
|
||||||
@@ -89,7 +89,7 @@ result = sql('''
|
|||||||
''', (course_name,))
|
''', (course_name,))
|
||||||
print(f"\n'{course_name}' 과목을 수강하는 학생 수: {result['student_count'].iloc[0]}")
|
print(f"\n'{course_name}' 과목을 수강하는 학생 수: {result['student_count'].iloc[0]}")
|
||||||
|
|
||||||
#
|
#%%
|
||||||
# 가장 많은 학생이 수강하는 과목 조회
|
# 가장 많은 학생이 수강하는 과목 조회
|
||||||
most_popular_course = sql('''
|
most_popular_course = sql('''
|
||||||
SELECT courses.name, COUNT(*) as enrollment_count
|
SELECT courses.name, COUNT(*) as enrollment_count
|
||||||
@@ -101,7 +101,7 @@ most_popular_course = sql('''
|
|||||||
''')
|
''')
|
||||||
print(f"\n가장 인기 있는 과목: {most_popular_course['name'].iloc[0]} (수강생 {most_popular_course['enrollment_count'].iloc[0]}명)")
|
print(f"\n가장 인기 있는 과목: {most_popular_course['name'].iloc[0]} (수강생 {most_popular_course['enrollment_count'].iloc[0]}명)")
|
||||||
|
|
||||||
#
|
#%%
|
||||||
# 최종 상태 출력
|
# 최종 상태 출력
|
||||||
print("\n최종 상태:")
|
print("\n최종 상태:")
|
||||||
print("Students:")
|
print("Students:")
|
||||||
@@ -110,4 +110,5 @@ print("\nCourses:")
|
|||||||
display(sql('SELECT * FROM courses'))
|
display(sql('SELECT * FROM courses'))
|
||||||
print("\nEnrollments:")
|
print("\nEnrollments:")
|
||||||
display(sql('SELECT * FROM enrollments'))
|
display(sql('SELECT * FROM enrollments'))
|
||||||
|
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user