views:

29

answers:

1

In python, I am populating a SQLITE data base using the importmany, so I can import tens of thousands of rows of data at once. My data is contained as a list of tuples. I had my database set up with the primary keys where I wanted them.

Problem I ran into was primary key errors would throw up an IntegrityError. If I handle the exception my script stops importing at the primary key conflict.
try:

try:
    self.curs.executemany("INSERT into towers values (NULL,?,?,?,?)",self.insertList)
except IntegrityError:
    print "Primary key error"
conn.commit()


So my questions are, in python using importmany can I:

1. Capture the values that violate the primary key?
2. Continue loading data after I get my primary key errors.

I get why it doesnt continue to load, because after the exception I commit the data to the database. I dont know how to continue where I left off however.

Unforutnley I cannot copy and paste all the code on this network, any help would be greatly appreciated. Right now I have no PKs set as a work around...

A: 

You could use lastrowid to get the point where you stopped:

http://docs.python.org/library/sqlite3.html#sqlite3.Cursor.lastrowid

If you use it, however, you can't use executemany.

Allan