current_app
เริ่มจากการติดตั้ง
Code: Select all
from flask import Flask, current_app
import psycopg2 as p
code ที่จะเอามาใช้คือ
Code: Select all
try:
except Exception as e:
current_app.logger.info(e)
response = 'error'
print(str(e))
return str(e)
และนี่คือ code เต็มๆ ใน database เรากำหนดให้ length ของ dns = 25 แต่ข้อมูลที่เอามาใส่ มากกว่า 25 ทำให้เกิด error
Code: Select all
@app.route('/')
def inserturl():
try:
dns_tm = 1234567891011121314151617181920212223242526
con = p.connect(host='127.0.0.1', database='test', user='postgres', password='0123456')
cur = con.cursor()
sql = """INSERT INTO ur (dns)VALUES ('%s') """ %( dns_tm)
sql = sql.encode('utf-8')
try:
cur.execute(sql)
id = int(cur.fetchone()[0])
con.commit()
return ('เพิ่มข้อมูลเรียบร้อยแล้ว')
except Exception as e:
current_app.logger.info(e)
con.rollback()
return str(e)
except Exception as e:
current_app.logger.info(e)
response = 'error'
print(str(e))
return str(e)
และนี่คือการแจ้ง error
Code: Select all
value too long for type character varying(25)