using databases and sql friend_id cur fetchone()[ countold countold exceptcur execute('''insert or ignore into people (nameretrievedvalues ? )'''friendconn commit(if cur rowcount ! print('error inserting account:',friendcontinue friend_id cur lastrowid countnew countnew if we end up in the except codeit simply means that the row was not foundso we must insert the row we use insert or ignore just to avoid errors and then call commit(to force the database to really be updated after the write is donewe can check the cur rowcount to see how many rows were affected since we are attempting to insert single rowif the number of affected rows is something other than it is an error if the insert is successfulwe can look at cur lastrowid to find out what value the database assigned to the id column in our newly created row storing the friend relationship once we know the key value for both the twitter user and the friend in the jsonit is simple matter to insert the two numbers into the follows table with the following codecur execute('insert or ignore into follows (from_idto_idvalues (??)'(idfriend_idnotice that we let the database take care of keeping us from "double-insertinga relationship by creating the table with uniqueness constraint and then adding or ignore to our insert statement here is sample execution of this programenter twitter accountor quitno unretrieved twitter accounts found enter twitter accountor quitdrchuck retrieving new accounts revisited enter twitter accountor quitretrieving new accounts revisited enter twitter accountor quitretrieving new accounts revisited enter twitter accountor quitquit