[공통교육-파이썬 기초] 데이터베이스 Python 연동
1. 학습한 내용
① HTTP Methods

② The Request Object

③ Redirects and Errors

④ SQLiteStudio (GUI)
- 설치 https://sqlitestudio.pl/
  
- 압축 해제 후 실행
  
- 데이터베이스 생성
     
- 테이블 생성
      
- Data 삽입
      
③ SQLite (CLI)
- Glitch에 내장된 SQLite를 사용하기 위해 Terminal 사용
  
- 데이터베이스 생성
  
- 테이블 생성
  
- 생성된 데이터베이스 파일의 상세정보
  
- Glitch 웹 사이트의 목록을 갱신
  
④ 수업 중 작성한 코드
from flask import Flask, request, redirect
app = Flask(__name__)
topics = [
  {"id":1, "title":"html", "body":"html is ...."},
  {"id":2, "title":"css", "body":"css is ...."},
  {"id":3, "title":"js", "body":"js is ...."}
]
nextId = 4
def template(content, id=None):
  liTags = ''
  for topic in topics:
    liTags = liTags + f'<li><a href="/read/{topic["id"]}/">{topic["title"]}</a></li>'
  return f'''
  <html>
    <body>
      <h1><a href="/">WEB</a></h1>
      <ol>
        {liTags}
      </ol>
      {content}
      <ul>
        <li><a href="/create/">create</a></li>
        <li>
          <form action="/delete/{id}/" method="POST">
            <input type="submit" value="delete">
          </form>
        </li>
      </ul>
    </body>
  </html>
  '''
@app.route("/")
def index():
  return template('<h2>Welcome</h2>Hello, WEB!')
@app.route("/read/<int:id>/")
def read(id):
  title = ''
  body = ''  
  for topic in topics :
    if topic['id'] == id:
      title = topic['title']
      body = topic['body']
      break;
  return template(f'<h2>{title}</h2>{body}', id)
@app.route('/create/')
def create():
  content = '''
    <form action="/create_process/" method="POST">
      <p><input type="text" name="title" placeholder="title"></p>
      <p><textarea name="body" placeholder="body"></textarea></p>
      <p><input type="submit" value="create"></p>
    </form>
  '''
  return template(content)
@app.route('/create_process/', methods=['POST'])
def create_process():
  global nextId
  title = request.form['title']
  body = request.form['body']
  newTopic = {"id":nextId, "title": title, "body": body}
  topics.append(newTopic)
  nextId = nextId + 1
  return redirect(f'/read/{nextId-1}/')
@app.route('/delete/<int:id>/', methods=['POST'])
def delete(id):
  for topic in topics:
    if topic['id'] == id:
      topics.remove(topic)
      break;
  return redirect('/')
 
# @app.route('/update/')
# def update():
#   return 'Update'
 
app.run()2. 학습내용 중 어려웠던 점
- Nothing
3. 해결방법
- Nothing
4. 학습소감
- 유명한 생활코딩의 이고잉 강사님의 강의 내용을 들을 수 있어서 영광입니다.
Author And Source
이 문제에 관하여([공통교육-파이썬 기초] 데이터베이스 Python 연동), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@s2angji/공통교육-파이썬-기초-데이터베이스-Python-연동저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)