Another crack at this: I have a frequent problem with python's psycopg2, perhaps in 2 parts. First, I consistently screw up string assignment, if anyone can lobotomize line 5 below for me, much appreciated (The first answer does work with 1 value.) Second, when I try to add a long sequence to a table, something like string-number pair in a dictionary, I have trouble dealing with exceptions. I don't care about losing the occasional weird word, but does anyone know how to make psycopg and postgres deal with errors without having to reestablish the connection, like MySQLdb? The commented version of the below works with MySQLdb, the comments make it work with Psycopg2 (and other postgres python modules?, am not wedded to psycopg2):
results = {'felicitas': 3, 'volumes': 8, 'acillevs': 1, 'mosaics': 13, 'perat\xe9': 1, 'representative': 6....}
for item in sorted(results):
try:
cur.execute("""insert into resultstab values ('%s', %d)""" % (item, results[item]))
print item, results[item]
# conn.commit()
except:
# conn=psycopg2.connect(user='bvm', database='wdb', password='redacted')
# cur=conn.cursor()
print 'choked on', item
continue
This must slow things down, could anyone give a suggestion for passing over formatting errors? Obviously the above chokes on apostrophes, but is there a way to make it pass over that without getting something like the following, or committing, reconnecting, etc?:
agreement 19
agreements 1
agrees 1
agrippa 9
choked on agrippa's
choked on agrippina
choked on agrippæ
choked on ague