본문 바로가기
IT/PYTHON

[9일차] MySQL + PYTHON

by GWLEE 2022. 7. 1.

 

import pymysql




def task1():
    conn = pymysql.connect(host='127.0.0.1', user='gyuwon', password='1234',db='sakila', charset='utf8mb4')

    con,cur,row =None,None,None
    film_id, title, description, release_year, language, category ="","","","","",""

    cur = conn.cursor()
    user_input_category = input("카테고리를 입력하세요.\n")

    my_sql_input_category = "'" + user_input_category + "'"

    find_move_by_category_query = "select FM.film_id,FM.title,FM.description,FM.release_year,LA.name,CT.name from film FM join language LA on FM.language_id = LA.language_id join film_category FC on FM.film_id = FC. film_id join category CT on FC.category_id = CT.category_id where CT.name =" + my_sql_input_category



    cur.execute(find_move_by_category_query)
    print('%0s %20s %30s %37s %10s %15s'%("film_id","title", "description", "release_year", "language", "category"))
    while(True) :

        row = cur.fetchone()
        if row==None :
            break
        film_id = row[0]
        title = row[1]
        description = row[2]
        release_year = row[3]
        language = row[4]
        category = row[5]


        if len(description) > 50:
            description=description[:51]





        print('%4s %25s %30s %10s %13s %15s' %(film_id, title, description, release_year, language, category))



    conn.close()


def task2():
    conn = pymysql.connect(host='127.0.0.1', user='gyuwon', password='1234',db='sakila', charset='utf8mb4')

    con,cur,row =None,None,None
    film_id, title, description, release_year, length, rating ="","","","","",""

    cur = conn.cursor()
    user_input_actor = input("배우를 입력하세요.\n")

    user_input_actor_first_name,user_input_actor_last_name = user_input_actor.split()

    user_input_actor_first_name = "'" + user_input_actor_first_name + "'"
    user_input_actor_last_name = "'" + user_input_actor_last_name + "'"

    find_move_actor_query ="select FL.film_id, FL.title, FL.description, FL.release_year, FL.length, FL.rating from film FL join film_actor FA on FL.film_id = FA.film_id join actor AC on FA.actor_id = AC.actor_id where first_name=" + user_input_actor_first_name + "and last_name="+ user_input_actor_last_name

    cur.execute(find_move_actor_query)
    print('%6s %20s %30s %42s %10s %15s'%("film_id", "title", "description", "release_year", "length", "rating"))
    while(True) :
        row = cur.fetchone()
        if row==None :
            break
        film_id = row[0]
        title = row[1]
        description = row[2]
        release_year = row[3]
        length = row[4]
        rating = row[5]
        if len(description) > 50:
            description=description[:51]

        print('%3s %31s %30s %10s %13s %15s'%(film_id, title, description, release_year, length, rating))

    conn.close()

def task3():
    conn = pymysql.connect(host='127.0.0.1', user='gyuwon', password='1234',db='sakila', charset='utf8mb4')
    con,cur,row =None,None,None
    title, store_id, first_name, last_name, address, count ="","","","","",""
    user_input_move = input("영화제목을 입력하세요.\n")
    cur = conn.cursor()

    cur.callproc('SP_GET_STORE',[user_input_move])
    print('%0s %20s %10s %10s %35s %30s'%("title", "store_id", "first_name", "last_name", "address", "count"))
    while(True) :

        row = cur.fetchone()
        if row==None :
            break
        title = row[0]
        store_id = row[1]
        first_name = row[2]
        last_name = row[3]
        address = row[4]
        count = row[5]
        print('%10s %9s %14s %10s %50s %13s'%(title, store_id, first_name, last_name, address, count))

    conn.close()







while (True):
    print('Select Menu\n')
    print('===================\n')
    print('a. Films by genre\n')
    print('b. Titles by actor\n')
    print('c. Stores by title\n')
    print('q. Quit\n')

    user_input_category = input("영화장르를 입력하세요.\n")

    if user_input_category == 'q':
        break
    elif user_input_category =='a':
        task1()
    elif user_input_category =='b' :
        task2()
    elif user_input_category == 'c':
        task3()
    else :
        print('잘못 입력하셨습니다.')

'IT > PYTHON' 카테고리의 다른 글

[27일차] 데이터 시각화 numpy / pandas  (0) 2022.07.27
[26일차] map python programmers  (0) 2022.07.26
[5일차] generate  (0) 2022.06.24
[5일차] 데코레이터(Decorator)  (0) 2022.06.24
[4일차] 클로저  (0) 2022.06.23

댓글