การเพิ่มข้อมูลลงในฐานข้อมูล python กับ flask framework

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
jirawoot
PHP VIP Members
PHP VIP Members
โพสต์: 3129
ลงทะเบียนเมื่อ: 17/06/2019 10:30 am

การเพิ่มข้อมูลลงในฐานข้อมูล python กับ flask framework

โพสต์ที่ยังไม่ได้อ่าน โดย jirawoot »

การเพิ่มข้อมูลลงในฐานข้อมูล python กับ flask framework
ขั้นตอนแรกให้สร้างฐานข้อมูล และตาราง ที่จะเก็บข้อมูล แล้วสรา้งฟอร์มหน้า home.html

โค้ด: เลือกทั้งหมด

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="container-fluid">
  <div class="row">
    <div class="col"></div>
    <div class="col" >
        <br>
        <h2>เพิ่มข้อมูล</h2>
        <form action="/showname" method="POST">
            <div class="form-group">
                <label>ชื่อ</label>
                <input type="text" class="form-control" name="firstname">
            </div>
            <div class="form-group">
                <label>สกุล</label>
                <input type="text" class="form-control" name="lastname">
            </div>
            <input type="submit" value="ตกลง" class="btn btn-success">
        </form>
    </div>
    <div class="col" ></div>
  </div>
</div>
</body>
</html>
Selection_017.png
Selection_017.png (55.15 KiB) Viewed 1687 times
จะได้หน้าฟอร์มดังรูปภาพ
Selection_015.png
Selection_015.png (8.54 KiB) Viewed 1687 times
ในหน้าไฟล์ index.py จะต้องเรียกใช้ flask กับ psycopg2 เพื่อที่จะ connect กับฐานข้อมูล ดังโค้ด

โค้ด: เลือกทั้งหมด

from flask import Flask
from flask import Flask, render_template
from flask import Flask, request
import psycopg2 as p

app=Flask(__name__)

@app.route('/')
def index():
    return render_template("home.html")

@app.route('/showname', methods=['POST'])
def show():
    try:
        con=p.connect(host='localhost',database='test', user='postgres',password='root')
        cur=con.cursor()
    except p.DatabaseError:
        print('Error  %s' %p.DatabaseError)
    firstname = request.form['firstname']
    lastname = request.form['lastname']
    sql="INSERT INTO test_python(name_first, name_last)VALUES ('%s','%s');"%(firstname,lastname)
    try:
        sql = sql.encode('utf-8')
        cur.execute(sql)
        con.commit()
        t="เพิ่มสำเร็จ"
    except:
        con.rollback()
        t = "เพิ่มไม่สำเร็จ"

     #name= dict(request.form.items())
    return render_template("showname.html",data=t)

if __name__ == '__main__':
   app.run()
Selection_016.png
Selection_016.png (64.06 KiB) Viewed 1687 times
สร้างไฟล์ showname.html ขึ้นมาเพื่อจะรู้สถานะการเพิ่ม

โค้ด: เลือกทั้งหมด

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{{data}}
</body>
</html>
แล้วรันโปรแกรมเสร็จให้กรอกชื่อกับนามสกุลและคลิก ตกลง
Selection_018.png
Selection_018.png (9.55 KiB) Viewed 1687 times
หน้า showname ก็ปรากฎสถานะการเพิ่มขึ้น
Selection_019.png
Selection_019.png (6.45 KiB) Viewed 1687 times
แล้วข้อมูลก็จะเพิ่มลงในฐานข้อมูลเป็นอันเสร็จเรียบร้อย
Selection_020.png
Selection_020.png (6.63 KiB) Viewed 1687 times
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: facebook.com [Crawler], Majestic-12 [Bot] และบุคลทั่วไป 82