重庆思庄Oracle、Redhat认证学习论坛
标题:
python 访问PG,异常处理
[打印本页]
作者:
郑全
时间:
2023-12-10 19:19
标题:
python 访问PG,异常处理
1.语句
[postgres@eulersvr ~]$ cat except.py
# _*_ coding:utf-8 _*_
import psycopg2
conn= psycopg2.connect(database="postgres",user="postgres",password="postgres",host="192.168.0.38",port="5432")
cursor=conn.cursor()
sql="""delete from emp1; """
params=(100,)
try:
cursor.execute(sql,params)
except psycopg2.Error as er:
print(er)
conn.commit()
cursor.close()
conn.close()
2.执行
[postgres@eulersvr ~]$ python3 except.py
relation "emp1" does not exist
LINE 1: delete from emp1;
^
[postgres@eulersvr ~]$
3.验证
[postgres@eulersvr ~]$ psql
psql (16.1)
Type "help" for help.
postgres=#
postgres=#
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+---------------------+----------+----------
public | emp | table | postgres
public | emp_id_seq | sequence | postgres
public | employees | table | postgres
public | employees_empid_seq | sequence | postgres
(4 rows)
我这里确实没有emp1 ,所以报错。
欢迎光临 重庆思庄Oracle、Redhat认证学习论坛 (http://bbs.cqsztech.com/)
Powered by Discuz! X3.2