我是菜鸟,因此无法通过psycopg2插入Postgresql。我有一个包含以下行的文件:
42::Dead Presidents (1995)::Action|Crime|Drama
43::Restoration::Drama
我正在尝试通过以下代码将这些行更改为insert语句:
import psycopg2
try:
db = psycopg2.connect("dbname='db' user='postgres' host='localhost' password='password'")
cur = db.cursor()
except:
print("Unable to connect to the database.")
for line in open('./movies.dat'):
(movie_id, movie_name, tags) = line.split('::')[0:3]
ypos = movie_name.rfind('(')
ypos2 = movie_name.rfind(')')
if ypos < 0:
cur.execute("insert into movies values (%s, %s, %s, null)", (movie_id, movie_name, tags))
else:
cur.execute("insert into movies values (%s, %s, %s, %s)", (movie_id, movie_name[0:ypos].strip(), tags, movie_name[ypos+1:ypos2].strip()))
不幸的是我收到错误:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
psycopg2.InternalError: current transaction is aborted, commands ignored until end of transaction block
我不知道为什么,或者如何调试psycopg2的一般错误。有人有任何有用的想法吗?
这是python 3.2.3和postgresql 9.2