โค้ด: เลือกทั้งหมด
@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='localhost', database='m052', user='postgres', password='')
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()
print ('เพิ่มข้อมูล เรียบร้อยแล้ว')
except:
con.rollback()
print('เพิ่มข้อมูล ผิดพลาด')
try:
sql1 = "SELECT * from test_python ORDER BY id ASC LIMIT 1"
sql1 = sql1.encode('utf-8')
cur.execute(sql1)
sid = cur.fetchall()
for row in sid:
idi = row[0]
return 'Name :' + name + '<br>'+ 'Lastname : ' + lastname +'<br>' + 'Color you like :' + color + '<br>'+'<META HTTP-EQUIV="Refresh" CONTENT="2;http://127.0.0.1:5000/enternew">'
except:
print('ส่งข้อมูล ผิดพลาด')
except p.DatabaseError:
print ('Error %s' % p.DatabaseError)