บทที่ 3 ขั้นตอนการเขียน python โดยใช้ Flask การรับข้อมูลผ่านทางหน้าฟอร์มแล้วบันทึกลงฐานข้อมูล

ขั้นตอนนี้เป็นการเพิ่มข้อมูลลงฐานข้อมูลหรือ SQL (เอส-คิว-แอล) โดยการรับค่าจากหน้าฟอร์ม ส่วนของ Python (ไพธอน) สามารถติดต่อกับฐานข้อมูลได้หลายตัว แต่จะยกตัวอย่างโปรแกรมที่ใช้ Postgresql 

ขั้นตอนที่ 1 สร้างหน้าฟอร์มรับข้อมูล ใช้ html เขียนหน้าฟอร์ม

โดยการรับข้อมูลผ่าน textbox แล้วกด submit

<form action = "{{ url_for('addrec') }}" method = "POST">
         <table border="0" width="50"  align="center">
         <thead>
          <tr align="center"></tr>
            <td width="50%" align="center"><h1>เพิ่มข้อมูล</h1></td>
            <tr align="center"></tr>
            <td width="50%">ชื่อ <input type = "text" name = "name_first" /></td>
            <tr align="center"></tr>
              <td width="50%">นามสกุล<input type = "text" name = "name_last" /></td>
            <tr align="center"></tr>
              <td width="50%">สีที่ชอบ<input type = "text" name = "favorite_color" /></td>
            <tr align="center"></tr>
         <td width="50%"><input type = "submit" value = "บันทึก" /></td>
         </thead>
         </table>
      </form>

 ตัวอย่างภาพ

สร้างหน้าฟอร์มรับข้อมูล ใช้ html
สร้างหน้าฟอร์มรับข้อมูล ใช้ html

 

ขั้นตอนที่ 2 สร้าง method Add เพื่อที่จะเขียนคำสั่งรับค่า แล้ว save ลงฐานข้อมูล

คำสั่งเปิดหน้าเพิ่มข้อมูล

@app.route('/enternew')
def new_student():
    return render_template('adduser.html')

ส่วนของ method Add รับค่าจากฟอร์ม adduser มาเป็น Post หรือ get แล้ว นำมาแปลงค่า แล้วเพิ่มลง ฐานข้อมูล

@app.route('/addrec', methods=['POST', 'GET'])
def addrec():
    if request.method == 'POST':
        name = request.form['name_first']
        lastname = request.form['name_last']
        color = request.form['favorite_color']
        try:
            con = p.connect(host='127.0.0.1', database='test', user='testerp', password='test123')
            cur = con.cursor()
            sql = "insert into test_python (name_first, name_last, favorite_color) values ('%s','%s','%s')" % (name, lastname, color)
            sql = sql.encode('utf-8')
            try:
                cur.execute(sql)
                con.commit()
                return 'Name :' + name + '<br>' + 'Color 
            except:
                con.rollback()
                print('เพิ่มข้อมูล ผิดพลาด')
        except p.DatabaseError:
             print ('Error %s' % p.DatabaseError)

 ตัวอย่างจากภาพ

method Add รับค่าจากฟอร์ม adduser
method Add รับค่าจากฟอร์ม adduser

ผลรับที่ได้จากการรัน

ฟอร์มรับข้อมูล
ฟอร์มรับข้อมูล

 

ข้อมูลใน ฐานข้อมูล
ข้อมูลใน ฐานข้อมูล

เป็นขั้นตอนการเพิ่มข้อมูล ลงฐานข้อมูล ด้วยภาษา python บทต่อไปเป็นการแสดง แก้ไข ลบ 

 

หัวเรื่อง
บทเรียน Python Framework Flask Form
หมวดหมู่
Python Framework Flask, Python Framework Flask
ฮิต
39614
ผู้สร้างเอกสาร
ae
วันที่สร้างเอกสาร
2017-06-27 18:40:26

python_flask_form.zip

ประเภทไฟล์ zip

ขนาดไฟล์ 8.28 KB

ผู้อัพโหลดไฟล์ ae

วันที่อัพโหลด 2017-06-27 11:26:29


กระทู้ล่าสุดจากเว็บบอร์ด
หัวข้อกระทู้
ตอบ
เปิดดู
ล่าสุด
การทำงานกับ Cookies ใน Selenium
โดย athirach.offcial จ 01 เม.ย. 2024 12:03 pm บอร์ด Software testing
0
120
จ 01 เม.ย. 2024 12:03 pm โดย athirach.offcial View Topic การทำงานกับ Cookies ใน Selenium
การเขียน Selenium ให้แจ้งเตือน Message Error
โดย athirach.offcial จ 01 เม.ย. 2024 11:59 am บอร์ด Software testing
0
113
จ 01 เม.ย. 2024 11:59 am โดย athirach.offcial View Topic การเขียน Selenium ให้แจ้งเตือน Message Error
เครื่องมือที่ช่วยในการทำ Automated test ใน python
โดย athirach.offcial จ 01 เม.ย. 2024 11:51 am บอร์ด Software testing
0
110
จ 01 เม.ย. 2024 11:51 am โดย athirach.offcial View Topic เครื่องมือที่ช่วยในการทำ Automated test ใน python
selenium เวอร์ชั่น ปัจจุบัน เป็นเวอร์ชั่นที่เท่าไหร่
โดย athirach.offcial จ 01 เม.ย. 2024 11:39 am บอร์ด Software testing
0
87
จ 01 เม.ย. 2024 11:39 am โดย athirach.offcial View Topic selenium เวอร์ชั่น ปัจจุบัน เป็นเวอร์ชั่นที่เท่าไหร่
ปัจจัยที่ทำให้เกิด Spam score ในเว็บไซต์
โดย athirach.offcial จ 01 เม.ย. 2024 11:27 am บอร์ด Share Knowledge
0
151
จ 01 เม.ย. 2024 11:27 am โดย athirach.offcial View Topic ปัจจัยที่ทำให้เกิด Spam score ในเว็บไซต์
Dell Optiplex 980 ไม่รองรับแรม Rank แบบ Single Rank
โดย mindphp จ 01 เม.ย. 2024 1:55 am บอร์ด ถาม - ตอบ คอมพิวเตอร์
0
349
จ 01 เม.ย. 2024 1:55 am โดย mindphp View Topic Dell Optiplex 980 ไม่รองรับแรม Rank แบบ Single Rank
การจัดประเภทของการ Test โปรแกรม
โดย athirach.offcial ส 30 มี.ค. 2024 12:54 pm บอร์ด Software testing
0
215
ส 30 มี.ค. 2024 12:54 pm โดย athirach.offcial View Topic การจัดประเภทของการ Test โปรแกรม
เทคนิคการเขียน Test Case ให้ครอบคลุม
โดย athirach.offcial ส 30 มี.ค. 2024 12:50 pm บอร์ด Software testing
0
169
ส 30 มี.ค. 2024 12:50 pm โดย athirach.offcial View Topic เทคนิคการเขียน Test Case ให้ครอบคลุม