데이터베이스 예제코드 (MySQL+AWS) - . 테이블에서 데이터 바로 추출하기

12. 테이블에서 데이터 바로 추출하기

  • 03_data/sakila-db-tables 폴더가 사전에 만들어져 있어야 함
SQL = "SHOW TABLES"
df = pd.read_sql(SQL, db)
df
Out[67]:
Tables_in_sakila
0 actor
1 actor_info
2 address
3 category
4 city
5 country
6 customer
7 customer_list
8 film
9 film_actor
10 film_category
11 film_list
12 film_text
13 inventory
14 language
15 nicer_but_slower_film_list
16 payment
17 rental
18 sales_by_film_category
19 sales_by_store
20 staff
21 staff_list
22 store
def save_to_csv(table_name):
    table_df = pd.read_sql("SELECT * FROM {table_name};".format(table_name=table_name), db)
    table_df.to_csv("03_data/sakila-db-tables/{table_name}.csv".format(table_name=table_name))
df["Tables_in_sakila"].apply(save_to_csv)
Out[70]:
0     None
1     None
2     None
3     None
4     None
5     None
6     None
7     None
8     None
9     None
10    None
11    None
12    None
13    None
14    None
15    None
16    None
17    None
18    None
19    None
20    None
21    None
22    None
Name: Tables_in_sakila, dtype: object
df
Out[66]:
customer_name
0 Robert
1 Peter