[英]How should I do exception handling in my database python code for no data fetched from server or connection time out
psycopg2.DatabaseError: could not receive data from server: Connection timed out psycopg2.DatabaseError:无法从服务器接收数据:连接超时
Wrap it inside try/except block.将它包裹在 try/except 块中。 like:
喜欢:
try:
conn = conn = dbapi.connect(user=<>,password=<>,host=<>,database=<>)
except psycopg2.DatabaseError:
<whatever code>
you can use this code in your:您可以在以下位置使用此代码:
def retrieve_data(db, table_name):
try:
comm ="SELECT * FROM {};".format(table_name)
with db.connect() as conn:
column_of_sql_table = conn.execute(comm).fetchall()
return pd.DataFrame(column_of_sql_table)
except Exception as e:
print(e)
return pd.DataFrame()
df = retrieve_data(db, table_name)
if not df.empty :
< do what ever you want>
else:
< rise error>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.