[英]InternalError: current transaction is aborted, commands ignored until end of transaction block ,on UNIQUE constraint
I tried to get executed with my except:
statement... while attempt to oppose the functionality of UNIQUE constraint..But ended with exceptional error.. The Postgresql database table already contains the row that I have used in 我试图用我的except:
语句执行...,同时试图反对UNIQUE约束的功能。但是以异常错误结束。.Postgresql数据库表已经包含我在其中使用的行
db.insert("The News","AparnaKumar",1995,234569654) db.insert(“新闻”,“ AparnaKumar”,1995,234569654)
but it works well on inserting unrepeated rows.. 但在插入未重复的行时效果很好。
import psycopg2
class database:
def __init__(self):
self.con=psycopg2.connect("dbname='book_store' user='postgres' password='5283' host='localhost' port='5432' ")
self.cur=self.con.cursor()
self.cur.execute("CREATE TABLE if not exists books(id SERIAL PRIMARY KEY,title TEXT NOT NULL UNIQUE,author TEXT NOT NULL,year integer NOT NULL,isbn integer NOT NULL UNIQUE)")
self.con.commit()
def insert(self,title,author,year,isbn):
try:
self.cur.execute("INSERT INTO books(title,author,year,isbn) VALUES(%s,%s,%s,%s)",(title,author,year,isbn))
self.con.commit()
except:
print("already exists..")
def view(self):
self.cur.execute("SELECT * FROM books")
rows=self.cur.fetchall()
print(rows)
def search(self,title=None,author=None,year=None,isbn=None):
self.cur.execute("SELECT * FROM books WHERE title=%s or author=%s or year=%s or isbn=%s",(title,author,year,isbn))
row=self.cur.fetchall()
print(row)
db=database()
db.insert("The News","AparnaKumar",1995,234569654)
db.view()
db.search(year=1995)
You can either modify your python function to rollback the transaction, or modify the sql to not insert a new row if a conflict occurs (postgresql versions 9.5+): 您可以修改python函数以回滚事务,或者修改sql以在发生冲突时不插入新行(PostgreSQL版本9.5+):
option 1: 选项1:
def insert(self,title,author,year,isbn):
try:
self.cur.execute("INSERT INTO books(title,author,year,isbn) VALUES(%s,%s,%s,%s)",(title,author,year,isbn))
self.con.commit()
except:
self.con.rollback()
print("already exists..")
option 2 (works for postgresql versions 9.5 or later): 选项2(适用于PostgreSQL 9.5或更高版本):
def insert(self,title,author,year,isbn):
try:
self.cur.execute("INSERT INTO books(title,author,year,isbn) VALUES(%s,%s,%s,%s) ON CONFLICT DO NOTHING",(title,author,year,isbn))
self.con.commit()
except:
print("already exists..")
Use a SAVEPOINT
. 使用SAVEPOINT
。
See the second question in the psycopg FAQ list . 请参阅psycopg常见问题解答列表中的第二个问题。
If you want to keep an transaction open, you should use a SAVEPOINT and ROLLBACK to that SAVEPOINT when the exception occurs. 如果要保持事务打开,则应在发生异常时使用SAVEPOINT和ROLLBACK到该SAVEPOINT。 But since you use a catchall exception (too broad), this will also happen on other errors. 但是,由于使用了包罗万象的异常(范围太广),因此在其他错误上也会发生这种情况。 So it gets a bit out of control. 因此它有点失控。
Perhaps a very simple solution is to check using a SELECT if the record you're about to insert already exists. 也许非常简单的解决方案是使用SELECT检查要插入的记录是否已经存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.