การเพิ่มข้อมูลลงในฐานข้อมูล 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 (55.15 KiB) Viewed 1725 times
จะได้หน้าฟอร์มดังรูปภาพ
- Selection_015.png (8.54 KiB) Viewed 1725 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 (64.06 KiB) Viewed 1725 times
สร้างไฟล์ showname.html ขึ้นมาเพื่อจะรู้สถานะการเพิ่ม
โค้ด: เลือกทั้งหมด
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{data}}
</body>
</html>
แล้วรันโปรแกรมเสร็จให้กรอกชื่อกับนามสกุลและคลิก ตกลง
- Selection_018.png (9.55 KiB) Viewed 1725 times
หน้า showname ก็ปรากฎสถานะการเพิ่มขึ้น
- Selection_019.png (6.45 KiB) Viewed 1725 times
แล้วข้อมูลก็จะเพิ่มลงในฐานข้อมูลเป็นอันเสร็จเรียบร้อย
- Selection_020.png (6.63 KiB) Viewed 1725 times
[b][size=150][color=#FF0000]การเพิ่มข้อมูลลงในฐานข้อมูล python กับ flask framework[/color][/size][/b]
ขั้นตอนแรกให้สร้างฐานข้อมูล และตาราง ที่จะเก็บข้อมูล แล้วสรา้งฟอร์มหน้า home.html
[code]
<!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>
[/code]
[attachment=3]Selection_017.png[/attachment]
จะได้หน้าฟอร์มดังรูปภาพ
[attachment=5]Selection_015.png[/attachment]
ในหน้าไฟล์ index.py จะต้องเรียกใช้ flask กับ psycopg2 เพื่อที่จะ connect กับฐานข้อมูล ดังโค้ด
[code]
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()
[/code]
[attachment=4]Selection_016.png[/attachment]
สร้างไฟล์ showname.html ขึ้นมาเพื่อจะรู้สถานะการเพิ่ม
[code]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{data}}
</body>
</html>
[/code]
แล้วรันโปรแกรมเสร็จให้กรอกชื่อกับนามสกุลและคลิก ตกลง
[attachment=2]Selection_018.png[/attachment]
หน้า showname ก็ปรากฎสถานะการเพิ่มขึ้น
[attachment=1]Selection_019.png[/attachment]
แล้วข้อมูลก็จะเพิ่มลงในฐานข้อมูลเป็นอันเสร็จเรียบร้อย
[attachment=0]Selection_020.png[/attachment]