0

私は初心者なので、psycopg2を介してPostgresqlに挿入するのに問題があります。次のような行を含むファイルがあります。

42::Dead Presidents (1995)::Action|Crime|Drama
43::Restoration::Drama

次のコードを使用して、これらの行を挿入ステートメントに変更しようとしています。

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の一般的なエラーをデバッグする理由や方法がわかりません。誰か役に立つアイデアがありますか?

これはpython3.2.3とpostgresql9.2です

4

1 に答える 1

1
except:
    print("Unable to connect to the database.")

あなたは致命的なエラーを無視しています。あなたがすべき:

  1. より有用なエラーメッセージ(実際のエラーを含む)を出力します
  2. 実行を停止します(このcatchステートメントを削除して例外を伝播させるか、エラーコードを返します)。
于 2012-09-15T21:17:48.250 に答える