Как сгенерировать предупреждение о недопустимых условиях вставки в psycopg2?Python

Программы на Python
Гость
Как сгенерировать предупреждение о недопустимых условиях вставки в psycopg2?

Сообщение Гость »


I have a table myuser with two columns (id serial primary, username varchar(20) unique not null). I am trying to make a Tkinter gui that takes "username" from a form and inserts it in the postgres table myuser.
I want to generate a warning when "username" field is left empty (or any other invalid requests) in the gui. How to do that ? Could not find any suitable answers in psycopg error lists.
I tried the following, but if I keep the username field in the gui empty, I don't see any warning, and the program runs without error. I expected to see a warning with this if condition.
Also, if there are other types of invalidity (> 20 characters), my solution would not work. So, what would be the best way for doing this ?

Код: Выделить всё

name = namefield.get()
try:
c.execute('''INSERT INTO myuser (username)
VALUES (%s)''', (name,)
)
if name == None:
print('Please insert a valid username')

except psycopg2.IntegrityError:
conn.rollback()
else:
conn.commit()
Many thanks for your help!


Источник: https://stackoverflow.com/questions/766 ... n-psycopg2

Вернуться в «Python»