My Note/프로젝트 관리

(flask_board)

db.py

import sqlite3

import click
from flask import current_app, g
from flask.cli import with_appcontext

get_db함수에서 sqlite3 데이터베이스 연결

def get_db():
# g 객체에 db가 없다면?
if 'db' not in g:

    g.db = sqlite3.connect(
        # 현재 애플리케이션의 데이터베이스로 설정
        current_app.config['DATABASE'],
        # 반환되는 각 row에 대해 선언된 형을 구문분석
        detect_types=sqlite3.PARSE_DECLTYPES
    )
    # 튜플 형식이 아닌 접근하는 데이터를 dictionary 타입과 비슷하게 키-값 쌍으로 사용할 수 있게 해줌
    g.db.row_factory = sqlite3.Row

return g.db

close_db 함수에서 데이터베이스 연결을 종료

def close_db(e=None):
# 글로벌 객체인 g에서 db를 추출
db = g.pop('db', None)

# db가 있다면 db를 종료
if db is not None:
    db.close()

웹 애플리케이션에 등록

schema.sql에서 작성한 쿼리를 통해 데이터베이스등록.

def init_db():
db = get_db()

# schema.sql의 파일을 열어 query를 실행
with current_app.open_resource('schema.sql') as f:
    db.executescript(f.read().decode('utf8'))

init-db command 함수: 명령어를 shell에서 실행한다면 함수를 실행

@click.command('init-db')
@with_appcontext
def init_db_command():
init_db()
click.echo('Initialized the database.')

웹 애플리케이션에 등록하기 위한 함수 init_app

def init_app(app):
# HTTP 요청이 완료되면 실행
app.teardown_appcontext(close_db)
# 새로운 shell 명령어를 추가
app.cli.add_command(init_db_command)

init.py

웹 애플리케이션이 실행 될 때, 데이터베이스를 초기화하는 구문

from . import db
db.init_app(app)

'My Note > 프로젝트 관리' 카테고리의 다른 글

run.sh shell scripts 파일로 flask 실행  (0) 2021.11.12
developer community css추가  (0) 2021.11.12
'Back' end  (0) 2021.11.09
배포를 위한 NGINX 명령어 -  (0) 2021.08.30
다시 빌드~  (0) 2021.08.29