๐Ÿ“ RDB

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(DataBase)์˜ ์ข…๋ฅ˜๋Š” ํฌ๊ฒŒ 1. ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(RDB, 2. NoSQL(Not Only SQL)๋กœ ๋‚˜๋‰œ๋‹ค.

RDB(Relation Database)๋Š” ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋‹ค.
๋‹ค์‹œ ๋งํ•ด, ํ‚ค(Key) - ๊ฐ’(Value)๋“ค์˜ ๊ฐ„๋‹จํ•œ ๊ด€๊ณ„๋ฅผ ํ…Œ์ด๋ธ”ํ™”ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋‹ค. RDB๋Š” ๋‹ค์Œ ํŠน์ง•์„ ๊ฐ€์ง„๋‹ค.

  • ๋ฐ์ดํ„ฐ ๋…๋ฆฝ์„ฑ์ด ๋†’๋‹ค.
  • ๊ณ ์ˆ˜์ค€์˜ DML์„ ์‚ฌ์šฉํ•ด์„œ, ๊ฒฐํ•ฉ, ์ œ์–‘, ํˆฌ์˜ ๋“ฑ์˜ ๊ด€๊ณ„ ์กฐ์ž‘์— ์˜ํ•ด ๋น„์•ฝ์ ์œผ๋กœ ํ‘œํ˜„ ๋Šฅ๋ ฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋“ค์˜ ๊ด€๊ณ„ ์กฐ์ž‘์— ์˜ํ•ด ์ž์œ ๋กญ๊ฒŒ ๊ตฌ์กฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ’ก RDB์˜ ์ข…๋ฅ˜

  • Oracle
  • MySQL
  • MS-SQL
  • DB2
  • Maria DB
  • Derby
  • SQLite

 

๐Ÿ“ RDB์™€ Flask์˜ ์ƒํ˜ธ์ž‘์šฉ

Flask์—์„œ RDB๋ฅผ ์—ฐ๋™ํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ ? Flask์—์„œ ์ž…๋ ฅ ๋ฐ›์€ ๋‚ด์šฉ๋“ค์„ DB์— ์ €์žฅํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

= ํšจ์œจ์ ์ธ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ ์ œ๊ณต

ํŒŒ์ด์ฌ์€ ์˜คํ”ˆ ์†Œ์Šค์™€ ์ƒ์šฉ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•œ ๋Œ€๋ถ€๋ถ„์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—”์ง„์„ ์œ„ํ•œ ํŒจํ‚ค์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
์•ž์œผ๋กœ์˜ ํฌ์ŠคํŒ…์—์„œ๋Š” ๊ทธ ์ค‘, sqlite3 ์™€ Flask ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์•ˆ์— ์žˆ๋Š” SQLAlchemy๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ง„ํ–‰ํ•œ๋‹ค.

SQLAlchemy๋Š” ํŒŒ์ด์ฌ ์ฝ”๋“œ์— DB์™€ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค.

[ ์ถœ์ฒ˜ : elice ]

 

๐Ÿ“ Flask - RDB ์˜ˆ์ œ : ๊ฒŒ์‹œํŒ ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿ’ก DB ์‚ฌ์šฉ์ž ์ถ”๊ฐ€

from flask import Flask, render_template, request, url_for, redirect
import sqlite3 # salite3

app = Flask(__name__)
conn = sqlite3.connect("database.db")   # splite3 db ์—ฐ๊ฒฐ
print("Opened database successfully")
conn.execute("CREATE TABLE IF NOT EXISTS Board(name TEXT, context TEXT)")   # Board ๋ผ๋Š” DB์ƒ์„ฑ
print("TABLE Created Successfully")
name = [
    ["Elice", 15],
    ["Dodo", 16],
    ["checher", 17],
    ["Queen", 18]
]
for i in range(len(name)):
    conn.execute(f"INSERT INTO Board(name,context) VALUES('{name[i][0]}', '{name[i][1]}')")  # Board DB์— ๋ฐ์ดํ„ฐ ์‚ฝ์ž…
conn.commit()   # ์ง€๊ธˆ๊ป ์ž‘์„ฑํ•œ SQL, DB์— ๋ฐ˜์˜ commit
conn.close()    # ์ž‘์„ฑ ๋‹คํ•œ DB๋Š” ๋‹ซ์•„์ค˜์•ผํ•จ close

# ================= ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ๋‹ค์‹œ Flask ์˜์—ญ ==========================

@app.route('/')
def board():
    con = sqlite3.connect("database.db")
    cur = con.cursor()
    cur.execute("SELECT * FROM Board")
    rows = cur.fetchall()

    print("DB: ")
    for i in range(len(rows)):
        print(rows[i][0] + ':' + rows[i][1])
    return render_template("board1.html", rows = rows)


@app.route("/search", methods=["GET","POST"])
def search():
    if request.method == "POST":
        name = request.form["name"] # search.html ๊ฐ€๋ณด๋ฉด, form์— name๋งŒ ๋ฐ›๊ธฐ๋กœ ํ•จ.
        con = sqlite3.connect("database.db")
        cur = con.cursor()
        cur.execute(f"SELECT * FROM Board WHERE name='{name}'")
        rows = cur.fetchall()
        print("DB : ")
        for i in range(len(rows)):
            print(rows[i][0] + ':' + rows[i][1])
        return render_template("search.html", rows=rows)
    else:
        return render_template("search.html")

@app.route("/add", methods=["GET","POST"])
def add():
    if request.method == "POST":
        try:
            name = request.form["name"]
            context = request.form["context"]

            # DB์— ์ ‘๊ทผํ•ด์„œ, ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ• ๋•Œ๋Š”, ์ง์ ‘ DB๋ฅผ ์—ด์–ด์•ผ๋˜๋Š”๋ฐ, ์œ— ๊ณผ์ •์ฒ˜๋Ÿผ, close๊นŒ์ง€ ํ•˜๊ธฐ ํž˜๋“œ๋‹ˆ๊น, ํ•˜๋Š” ๋ฐฉ์‹, ๊ฒฐ๊ณผ๋Š” ๊ฐ™์€ ๊ฒƒ !
            with sqlite3.connect("database.db") as con:
                cur = con.cursor()
                cur.execute(f"INSERT INTO Board(name,context) VALUES('{name}','{context}')")
                con.commit()
        except:
            con.rollback()  # DB ๋กค๋ฐฑํ•จ์ˆ˜, SQL์ด ์˜ค๋ฅ˜๋‚˜๋ฉด, ๋ฐ˜์˜์ „, ์ด์ „ ์ƒํƒœ๋กœ ๋Œ๋ฆฌ๋Š” ๊ฒƒ
        finally:
            return redirect(url_for("board"))
    else:
        return render_template("add.html")


if __name__ == '__main__':
    app.run()
<!-- ./templates/board1.html -->

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="Generator" content="EditPlus®">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>SQLite3 ๊ฒŒ์‹œํŒ ๋“ฑ๋ก</title>

    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h3>๊ฒŒ์‹œํŒ</h3>
    <h4><a href="{{url_for('add')}}">์ถ”๊ฐ€</a> <a href="{{url_for('search')}}">๊ฒ€์ƒ‰</a><br><br>๋ชฉ๋ก</h4>
    <table border=1 width="600" align="center">
        <thead>
            <td>์ด๋ฆ„</td>
            <td>๋‚ด์šฉ</td>
        </thead>
        {% for row in rows %}
        <tr>
            <td>{{ row[0] }}</td>
            <td>{{ row[1] }}</td>
        </tr>
        {% endfor %}
    </table>
</body>

</html>
<!-- ./templates/add.html -->

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="Generator" content="EditPlus®">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>SQLite3 ๊ฒŒ์‹œํŒ ๋“ฑ๋ก</title>

    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h3>๊ฒŒ์‹œํŒ</h3>
    <h4>์ถ”๊ฐ€</h4>
    <form action="/add" method="POST">
        ์ด๋ฆ„<br>
        <input type="text" name="name" /><br>
        ๋‚ด์šฉ<br>
        <input type="text" name="context" style="text-align:center; width:400px; height:100px;" /><br><br>
        <input type="submit" value="๊ฒŒ ์‹œ" /><br>
    </form>
</body>

</html>
<!-- ./templates/search.html -->

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="Generator" content="EditPlus®">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>SQLite3 ๊ฒŒ์‹œํŒ ๋“ฑ๋ก</title>

    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <form action="/search" method="POST">
        <input type="text" name="name" />
        <input type="submit" value="๊ฒ€ ์ƒ‰" /><br>
    </form>
    <h4>๊ฒ€์ƒ‰๊ฒฐ๊ณผ</h4>
    {% if rows%}
    <table border=1 width="600" align="center">
        <thead>
            <td>์ด๋ฆ„</td>
            <td>๋‚ด์šฉ</td>
        </thead>
        {% for row in rows %}
        <tr>
            <td>{{ row[0] }}</td>
            <td>{{ row[1] }}</td>
        </tr>
        {% endfor %}
        {% else %}
        <p> ๊ฒ€์ƒ‰๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. </p>
        {% endif %}
    </table>
</body>

</html>

[ ์ถœ์ฒ˜ : elice ]

 

๐Ÿ’ก ์ค‘๋ณต ์‚ฌ์šฉ์ž ์ œ์–ด ( ์œ„์—, DB ์‚ฌ์šฉ์ž ์ถ”๊ฐ€์—์„œ ๋ณ€ํ•œ ์ฝ”๋“œ๋Š” ๋ช‡ ์—†์Œ

from flask import Flask, render_template, request, url_for, redirect
import sqlite3 # salite3

app = Flask(__name__)
conn = sqlite3.connect("database.db")   # splite3 db ์—ฐ๊ฒฐ
print("Opened database successfully")
conn.execute("CREATE TABLE IF NOT EXISTS Board(name TEXT, context TEXT)")   # Board ๋ผ๋Š” DB์ƒ์„ฑ
print("TABLE Created Successfully")
name = [
    ["Elice", 15],
    ["Dodo", 16],
    ["checher", 17],
    ["Queen", 18]
]
for i in range(len(name)):
    conn.execute(f"INSERT INTO Board(name,context) VALUES('{name[i][0]}', '{name[i][1]}')")  # Board DB์— ๋ฐ์ดํ„ฐ ์‚ฝ์ž…
conn.commit()   # ์ง€๊ธˆ๊ป ์ž‘์„ฑํ•œ SQL, DB์— ๋ฐ˜์˜ commit
conn.close()    # ์ž‘์„ฑ ๋‹คํ•œ DB๋Š” ๋‹ซ์•„์ค˜์•ผํ•จ close

# ================= ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ๋‹ค์‹œ Flask ์˜์—ญ ==========================

@app.route('/')
def board():
    con = sqlite3.connect("database.db")
    cur = con.cursor()
    cur.execute("SELECT * FROM Board")
    rows = cur.fetchall()

    print("DB: ")
    for i in range(len(rows)):
        print(rows[i][0] + ':' + rows[i][1])
    return render_template("board1.html", rows = rows)


@app.route("/search", methods=["GET","POST"])
def search():
    if request.method == "POST":
        name = request.form["name"] # search.html ๊ฐ€๋ณด๋ฉด, form์— name๋งŒ ๋ฐ›๊ธฐ๋กœ ํ•จ.
        con = sqlite3.connect("database.db")
        cur = con.cursor()
        cur.execute(f"SELECT * FROM Board WHERE name='{name}'")
        rows = cur.fetchall()
        print("DB : ")
        for i in range(len(rows)):
            print(rows[i][0] + ':' + rows[i][1])
        return render_template("search.html", rows=rows)
    else:
        return render_template("search.html", msg ="๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")

@app.route("/add", methods=["GET","POST"])
def add():
    if request.method == "POST":

        name = request.form["name"]
        context = request.form["context"]

        # DB์— ์ ‘๊ทผํ•ด์„œ, ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ• ๋•Œ๋Š”, ์ง์ ‘ DB๋ฅผ ์—ด์–ด์•ผ๋˜๋Š”๋ฐ, ์œ— ๊ณผ์ •์ฒ˜๋Ÿผ, close๊นŒ์ง€ ํ•˜๊ธฐ ํž˜๋“œ๋‹ˆ๊น, ํ•˜๋Š” ๋ฐฉ์‹, ๊ฒฐ๊ณผ๋Š” ๊ฐ™์€ ๊ฒƒ !
        with sqlite3.connect("database.db") as con:
            cur = con.cursor()
            cur.execute(f"SELECT count(*) FROM Board WHERE name='{name}'")
            
            # ํšŒ์› ํ•œ๋ช…, ์ถ”๊ฐ€ํ• ๋ผ ํ–ˆ๋Š”๋ฐ, ๊ทธ์ „์— ๋“ค์–ด์˜จ, name๊ฐ’์ด๋ž‘ ๊ฐ™์€ ์ด๋ฆ„์ด DB์— ์žˆ์œผ๋ฉด, ์ค‘๋ณตํšŒ์›์ž์ด๋ฏ€๋กœ, ๋ชปํ•˜๊ฒŒ ์ œ์–ดํ•จ
            if cur.fetchall()[0][0] == 0:   # ์ค‘๋ณต์ด๋ฆ„์ด ์—†์œผ๋ฉด
                cur.execute(f"INSERT INTO Board(name,context) VALUES('{name}','{context}')")
                con.commit()
                cur.execute("SELECT * FROM Board")
                rows = cur.fetchall()
                return render_template("board1.html",rows= rows)        
            else:   # ์ค‘๋ณต์ด๋ฆ„์ด ์žˆ์œผ๋ฉด
                return render_template("add.html",msg = "์ค‘๋ณต์‚ฌ์šฉ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.")
    else:
        return render_template("add.html")


if __name__ == '__main__':
    app.run()
<!-- ./templates/board1.html -->

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="Generator" content="EditPlus®">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>SQLite3 ๊ฒŒ์‹œํŒ ๋“ฑ๋ก</title>

    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h3>๊ฒŒ์‹œํŒ</h3>
    <h4><a href="{{url_for('add')}}">์ถ”๊ฐ€</a> <a href="{{url_for('search')}}">๊ฒ€์ƒ‰</a><br><br>๋ชฉ๋ก</h4>
    <table border=1 width="600" align="center">
        <thead>
            <td>์ด๋ฆ„</td>
            <td>๋‚ด์šฉ</td>
        </thead>
        {% for row in rows %}
        <tr>
            <td>{{ row[0] }}</td>
            <td>{{ row[1] }}</td>
        </tr>
        {% endfor %}
    </table>
</body>

</html>
<!-- ./templates/add.html -->

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="Generator" content="EditPlus®">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>SQLite3 ๊ฒŒ์‹œํŒ ๋“ฑ๋ก</title>

    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h3>๊ฒŒ์‹œํŒ</h3>
    <h4>์ถ”๊ฐ€</h4>
    <form action="/add" method="POST">
        ์ด๋ฆ„<br>
        <input type="text" name="name" /><br>
        ๋‚ด์šฉ<br>
        <input type="text" name="context" style="text-align:center; width:400px; height:100px;" /><br><br>
        <input type="submit" value="๊ฒŒ ์‹œ" /><br>
    </form>
    {% if msg %}
    <p> {{ msg }} </p>
    {% endif %}
</body>

</html>
<!-- ./templates/reaserch.html -->

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="Generator" content="EditPlus®">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>SQLite3 ๊ฒŒ์‹œํŒ ๋“ฑ๋ก</title>

    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <form action="/search" method="POST">
        <input type="text" name="name" />
        <input type="submit" value="๊ฒ€ ์ƒ‰" /><br>
    </form>
    <h4>๊ฒ€์ƒ‰๊ฒฐ๊ณผ</h4>
    {% if rows%}
    <table border=1 width="600" align="center">
        <thead>
            <td>์ด๋ฆ„</td>
            <td>๋‚ด์šฉ</td>
        </thead>
        {% for row in rows %}
        <tr>
            <td>{{ row[0] }}</td>
            <td>{{ row[1] }}</td>
        </tr>
        {% endfor %}
        {% elif msg %}
        <p> {{ msg }} </p>
        {% else %}
        <p> ๊ฒ€์ƒ‰๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. </p>
        {% endif %}
    </table>
</body>

</html>

[ ์ถœ์ฒ˜ : elice ]

 

๐Ÿ’ก ๊ฒŒ์‹œํŒ ๋‚ด์šฉ ์ƒ์„ฑ ๋ฐ ์กฐํšŒ

์œ„์—์„œ, DB ์‚ฌ์šฉ์ž ์ƒ์„ฑ, ์ค‘๋ณต์ œ๊ฑฐ ๋‚ด์šฉ๊ณผ ๋™์ผ

 

๐Ÿ’ก ๊ฒŒ์‹œํŒ ๋‚ด์šฉ ์ˆ˜์ • ๋ฐ ์‚ญ์ œ

#DATABASE
from flask import Flask, render_template, request, url_for, redirect
import sqlite3
app = Flask(__name__)
conn = sqlite3.connect('database.db')
print ("Opened database successfully")
conn.execute("DROP TABLE IF EXISTS Board")  # Board ํ…Œ์ด๋ธ”์ด ๊ธฐ์กด์— ์žˆ๋‹ค๋ฉด ์‚ญ์ œ (๋งค๋ฒˆ, ๋™์ผํ•œ ํŒŒ์ผ์—์„œ ์‹คํ–‰ํ•˜๋ฉด, ๋‚ด์šฉ์ด ๊ฒน์ณ์„œ ๋งŒ๋“ฆ)
conn.execute('CREATE TABLE IF NOT EXISTS Board (name TEXT, context TEXT)')  # Board ํ…Œ์ด๋ธ”์ด ๊ธฐ์กด์— ์—†๋‹ค๋ฉด ์ƒ์„ฑ
print ("Table created successfully")
name = [['Elice', 15], ['Dodo', 16], ['Checher', 17], ['Queen', 18]]
for i in range(4):
    conn.execute(f"INSERT INTO Board(name, context) VALUES('{name[i][0]}', '{name[i][1]}')")
conn.commit()
conn.close()

# root = home
@app.route('/')
def board():
    con = sqlite3.connect("database.db")
    cur = con.cursor()
    cur.execute("select * from Board")
    rows = cur.fetchall()
    print("DB:")
    for i in range(len(rows)):
            print(rows[i][0] + ':' + rows[i][1])
    return render_template('board1.html', rows = rows)

# ๊ฒŒ์‹œ๋ฌผ ์กฐํšŒ (Read)
@app.route('/search', methods = ['GET', 'POST'])
def search():
    if request.method == 'POST':
        name = request.form['name']
        con = sqlite3.connect("database.db")
        cur = con.cursor()
        cur.execute(f"SELECT * FROM Board WHERE name='{name}' or context='{name}'")
        rows = cur.fetchall()
        print("DB:")
        for i in range(len(rows)):
            print(rows[i][0] + ':' + rows[i][1])
        return render_template('search.html', rows = rows)
    else:
        return render_template('search.html')

# ๊ฒŒ์‹œ๋ฌผ ์ƒ์„ฑ (Create)
@app.route('/add', methods = ['GET', 'POST'])
def add():
    if request.method == 'POST':
        try:
            name = request.form['name']
            context = request.form['context']
            with sqlite3.connect("database.db") as con:
                cur = con.cursor()
                cur.execute(f"INSERT INTO Board (name, context) VALUES ('{name}', '{context}')")
                con.commit()
        except:
            con.rollback()
        finally : 
            con.close()
            return redirect(url_for('board'))
    else:
        return render_template('add.html')
# ์œ„์— ์กฐํšŒ, ์ƒ์„ฑ์€ ์ด์ „๊ณผ ๋™์ผ 

# ๊ฒŒ์‹œ๋ฌผ ๋‚ด์šฉ ๊ฐฑ์‹ (Update)
@app.route("/update/<uid>", methods=["GET","POST"])
def update(uid):
    if request.method == "POST":
        name = request.form["name"]
        context = request.form["context"]
        
        # ๋‚ด์šฉ ๊ฐฑ์‹ ํ•˜๊ณ 
        with sqlite3.connect("database.db") as con:
            cur = con.cursor()  # connectionํ•œ db์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด, cursor ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ
            cur.execute(f"UPDATE Board SET name='{name}', context='{context}' WHERE name='{uid}'")
            con.commit()

        return redirect(url_for("board"))   # ๊ฐฑ์‹ ๋˜์—ˆ๋Š”์ง€, boardํ•จ์ˆ˜ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•ด์„œ, / ํŽ˜์ด์ง€ ๋ Œ๋”๋ง
    else:
        con = sqlite3.connect("database.db")
        cur = con.cursor()
        cur.execute(f"SELECT * FROM Board WHERE name='{uid}'")
        row = cur.fetchall()
        return render_template("update.html",row=row)

@app.route("/delete/<uid>")
def delete(uid):
    # ๋“ค์–ด์˜จ uid ๊ฐ’์ด๋ž‘ name์ด๋ž‘ delete ์—ฐ์‚ฐํ•˜๊ณ  ๋ฐ˜์˜
    with sqlite3.connect("database.db") as con:
        cur = con.cursor()
        cur.execute(f"DELETE FROM Board WHERE name='{uid}'")
        con.commit()

    return redirect(url_for('board'))  # ์‚ญ์ œ ๋ฐ˜์˜ํ•˜๊ณ , ๋ฐ˜์˜๋ฌ๋Š”์ง€, boardํ•จ์ˆ˜ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ, / ํŽ˜์ด์ง€ ๋ Œ๋”๋ง

if __name__ == '__main__':
    app.run()
<!-- ./templates/board1.html -->

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="Generator" content="EditPlus®">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>SQLite3 ๊ฒŒ์‹œํŒ ๋“ฑ๋ก</title>

    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h3>๊ฒŒ์‹œํŒ</h3>
    <h4><a href="{{url_for('add')}}">์ถ”๊ฐ€</a> <a href="{{url_for('search')}}">๊ฒ€์ƒ‰</a><br><br>๋ชฉ๋ก</h4>
    <table border=1 width="600" align="center">
        <thead>
            <td>์ด๋ฆ„</td>
            <td>๋‚ด์šฉ</td>
            <td>์ˆ˜์ •/์‚ญ์ œ</td>
        </thead>
        {% for row in rows %}
        <tr>
            <td>{{ row[0] }}</td>
            <td>{{ row[1] }}</td>
            <td><a href="{{url_for('update', uid = row[0])}}">์ˆ˜์ •</a> <a
                    href="{{url_for('delete', uid = row[0])}}">์‚ญ์ œ</a></td>
        </tr>
        {% endfor %}
    </table>
</body>

</html>
<!-- ./templates/add.html -->

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="Generator" content="EditPlus®">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>SQLite3 ๊ฒŒ์‹œํŒ ๋“ฑ๋ก</title>

    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h3>๊ฒŒ์‹œํŒ</h3>
    <h4>์ถ”๊ฐ€</h4>
    <form action="/add" method="POST">
        ์ด๋ฆ„<br>
        <input type="text" name="name" /><br>
        ๋‚ด์šฉ<br>
        <input type="text" name="context" style="text-align:center; width:400px; height:100px;" /><br><br>
        <input type="submit" value="๊ฒŒ ์‹œ" /><br>
    </form>
</body>

</html>
<!-- ./templates/research.html -->

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="Generator" content="EditPlus®">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>SQLite3 ๊ฒŒ์‹œํŒ ๋“ฑ๋ก</title>

    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <form action="/search" method="POST">
        <input type="text" name="name" />
        <input type="submit" value="๊ฒ€ ์ƒ‰" /><br>
    </form>
    <h4>๊ฒ€์ƒ‰๊ฒฐ๊ณผ</h4>
    {% if rows%}
    <table border=1 width="600" align="center">
        <thead>
            <td>์ด๋ฆ„</td>
            <td>๋‚ด์šฉ</td>
        </thead>
        {% for row in rows %}
        <tr>
            <td>{{ row[0] }}</td>
            <td>{{ row[1] }}</td>
        </tr>
        {% endfor %}
        {% else %}
        <p> ๊ฒ€์ƒ‰๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. </p>
        {% endif %}
    </table>
</body>

</html>
<!-- ./templates/update.html -->

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="Generator" content="EditPlus®">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>SQLite3 ๊ฒŒ์‹œํŒ ๋“ฑ๋ก</title>

    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h3>๊ฒŒ์‹œํŒ</h3>
    <h4>์ˆ˜์ •</h4>
    <form action="" method="POST">
        ์ด๋ฆ„<br>
        <input type="text" name="name" /><br>
        ๋‚ด์šฉ<br>
        <input type="text" name="context" style="text-align:center; width:400px; height:100px;" /><br><br>
        <input type="submit" value="์ˆ˜ ์ •" /><br>
    </form>
    <h4>๊ธฐ์กด</h4>
    <table border=1 width="600" align="center">
        <thead>
            <td>์ด๋ฆ„</td>
            <td>๋‚ด์šฉ</td>
        </thead>
        <tr>
            <td>{{ row[0][0] }}</td>
            <td>{{ row[0][1] }}</td>
        </tr>
    </table>
</body>

</html>

[ ์ถœ์ฒ˜ : elice ]

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts