I've written this code that seems to work and I'd like to know how to get
the record that causes the abort.  Apparently 'executemany' doesn't
support lastrow?  And if it doesn't, any suggestions?

--8<---------------cut here---------------start------------->8---
def table_load(datafile,name,conn,dbh):
    print "processing table ",name
    conn.execute("PRAGMA table_info("+ name +")")
    #parse the resultset to get the col name
    cols= [ x[1] for x in conn.fetchall()]
    cv= ("?" * len(cols))
    with open(datafile,'r') as fin: 
        dr = csv.reader(fin, delimiter='|') 
        to_db = [tuple(i) for i in dr]
        print "Records read in: ",  len(to_db)
    cl=','.join(cols)
    cvv=','.join(cv)
    try:
        sql = "insert into %s (%s) values(%s)" %(name, cl, cvv)
        conn.executemany(sql, to_db)
        dbh.commit()
    except sq.IntegrityError:
        print('Record already exists') # but which record???
        dbh.rollback()
    finally:
        sql= "select count(*) from %s;" %(name)
        (row_cnt,) = conn.execute(sql).fetchone()
        print "rows inserted ", row_cnt
--8<---------------cut here---------------end--------------->8---

And do tell if I'm doing this try catch bits correctly please.


 sivaram
 -- 

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to