flask + sqlite3 TypeError, sqlite3 AUTOINCREMENT 관련해 질문 드립니다.

flask과 sqlite3을 이용한 crud를 구현에 염두를 두고 있습니다.
파일을 실행하면 열리는 http://127.0.0.1:5000/ 에 접속하면 이러한 에러가 나네요

TypeError
TypeError: The view function for 'index' did not return a valid response. The function either returned None or ended without a return statement.

플라스크의 기본만 아는 상태로 작업중이다 보니 어디가 에러인지 도통 감을 못잡겠어서 질문을 드립니다.
+추가적으로 sqlite3에서 tabl을 생성할 때 Row의 속성에 INTEGER PRIMARY KEY AUTOINCREMENT 값을 주면 자동으로 값이 증가한다고 알고 있는데 그렇다면 insert부분에선 id(위에서 autoincrement를 설정해놓은 row)값을 insert하지 않아도 되는건가요? 제 경우는 id값을 제외한 나머지 값들만 insert를 한 경우 id값이 None 이라고 뜹니다..

사용한 코드입니다. 글의 문제와 관계없이 코드에 대한 모든 피드백은 적극 환영합니다..!

from flask import Flask, request, redirect
import sqlite3

app = Flask(__name__)

db = '_test.db'

def setDb(title, body):
    conn = sqlite3.connect(db)
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS topic (id INTEGER PRIMARY KEY AUTOINCREMENT, title text, body text)")
    createRow(title, body)
    readRow(all)
    conn.commit()
    conn.close()
    return print("DB 초기 세팅 완료")

def createRow(title, body):
    conn = sqlite3.connect(db)
    cursor = conn.cursor()
    cursor.execute(f'''REPLACE INTO topic(title, body) VALUES(?,?)''',
        (title, body)
    )
    readRow(all)
    conn.commit()
    conn.close()
    return print("Row 생성 완료")

def readRow(opt="all"):
    conn = sqlite3.connect(db)
    cursor = conn.cursor()

    if opt == all:
        cursor.execute("SELECT * FROM topic")
    else:
        cursor.execute(f"SELECT body FROM topic WHERE id = '{id}'")

    rows = cursor.fetchall()
    print(rows)
    conn.close()
    return rows

def updateRow(id, title, body):
    conn = sqlite3.connect(db)
    cursor = conn.cursor()
    cursor.execute(f"UPDATE topic SET title = '{title}', body = '{body}' WHERE id = {id}")
    readRow(all)
    conn.commit()
    conn.close()
    return print("수정 완료")

def delRow(id):
    conn = sqlite3.connect(db)
    cursor = conn.cursor()

    if id == all:
        cursor.execute("DELETE FROM topic")
    else:
        cursor.execute(f"DELETE FROM topic WHERE id = {id}")
    readRow(all)
    conn.commit()
    conn.close()
    return print("삭제 완료")

@app.route('/')
def index():
    return setDb("hello world", "lorem ipsum is dummy text for web page development testing and more dummy text")


@app.route('/read/<int:id>/')
def read(id):
    return readRow(id)


@app.route('/create/', methods=['GET', 'POST'])
def create():
    form = """
    <form action="/create/" method="POST">
        <input type="text" name="id" placeholder="id">
        <input type="text" name="title" placeholder="title">
        <input type="submit" value="create">
    </form>
    """
    return createRow(request.form['id'], request.form['title'], request.form['body'])


@app.route('/update/<int:id>/', methods=['GET', 'POST'])
def update(id):
    form = """
    <form action="/update/{id}/" method="POST">
        <input type="text" name="title" placeholder="title">
        <input type="text" name="body" placeholder="body">
        <input type="submit" value="update">
    </form>
    """.format(id=id)
    return updateRow(id, request.form['title'], request.form['body'])


@app.route('/delete/<int:id>/', methods=['POST'])
def delete(id):
    return delRow(id)


app.run(debug=True)

lesh454612 314

M 2022년 7월 29일 11:33 오전

목록으로
1개의 답변이 있습니다. 1 / 1 Page

해결했습니다!!

LESH

2022년 8월 1일 4:43 오후